Skip to content

Commit

Permalink
Merge pull request #23 from NetApp/integration/main
Browse files Browse the repository at this point in the history
Sync bitbucket and GitHub
  • Loading branch information
wenjun666 authored Dec 15, 2020
2 parents 6965ad8 + 3e1571b commit 239ce86
Show file tree
Hide file tree
Showing 8 changed files with 633 additions and 1 deletion.
40 changes: 40 additions & 0 deletions cloudmanager/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,43 @@ func (c *Client) getWorkingEnvironmentDetail(d *schema.ResourceData) (workingEnv
}
return workingEnvDetail, nil
}

// read working environemnt information and return the details
func (c *Client) getWorkingEnvironmentDetailForSnapMirror(d *schema.ResourceData) (workingEnvironmentInfo, workingEnvironmentInfo, error) {
var sourceWorkingEnvDetail workingEnvironmentInfo
var destWorkingEnvDetail workingEnvironmentInfo
var err error

if a, ok := d.GetOk("source_working_environment_id"); ok {
WorkingEnvironmentID := a.(string)
sourceWorkingEnvDetail, err = c.findWorkingEnvironmentByID(WorkingEnvironmentID)
if err != nil {
return workingEnvironmentInfo{}, workingEnvironmentInfo{}, fmt.Errorf("Cannot find working environment by source_working_environment_id %s", WorkingEnvironmentID)
}
} else if a, ok = d.GetOk("source_working_environment_name"); ok {
sourceWorkingEnvDetail, err = c.findWorkingEnvironmentByName(a.(string))
if err != nil {
return workingEnvironmentInfo{}, workingEnvironmentInfo{}, fmt.Errorf("Cannot find working environment by source_working_environment_name %s", a.(string))
}
log.Printf("Get environment id %v by %v", sourceWorkingEnvDetail.PublicID, a.(string))
} else {
return workingEnvironmentInfo{}, workingEnvironmentInfo{}, fmt.Errorf("Cannot find working environment by source_working_environment_id or source_working_environment_name")
}

if a, ok := d.GetOk("destination_working_environment_id"); ok {
WorkingEnvironmentID := a.(string)
destWorkingEnvDetail, err = c.findWorkingEnvironmentByID(WorkingEnvironmentID)
if err != nil {
return workingEnvironmentInfo{}, workingEnvironmentInfo{}, fmt.Errorf("Cannot find working environment by destination_working_environment_id %s", WorkingEnvironmentID)
}
} else if a, ok = d.GetOk("destination_working_environment_name"); ok {
destWorkingEnvDetail, err = c.findWorkingEnvironmentByName(a.(string))
if err != nil {
return workingEnvironmentInfo{}, workingEnvironmentInfo{}, fmt.Errorf("Cannot find working environment by destination_working_environment_name %s", a.(string))
}
log.Printf("Get environment id %v by %v", destWorkingEnvDetail.PublicID, a.(string))
} else {
return workingEnvironmentInfo{}, workingEnvironmentInfo{}, fmt.Errorf("Cannot find working environment by destination_working_environment_id or destination_working_environment_name")
}
return sourceWorkingEnvDetail, destWorkingEnvDetail, nil
}
1 change: 1 addition & 0 deletions cloudmanager/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Provider() terraform.ResourceProvider {
"netapp-cloudmanager_aggregate": resourceAggregate(),
"netapp-cloudmanager_volume": resourceCVOVolume(),
"netapp-cloudmanager_cifs_server": resourceCVOCIFS(),
"netapp-cloudmanager_snapmirror": resourceCVOSnapMirror(),
"netapp-cloudmanager_nss_account": resourceCVONssAccount(),
},
DataSourcesMap: map[string]*schema.Resource{
Expand Down
255 changes: 255 additions & 0 deletions cloudmanager/resource_netapp_cloudmanager_snapmirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
package cloudmanager

import (
"log"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceCVOSnapMirror() *schema.Resource {
return &schema.Resource{
Create: resourceCVOSnapMirrorCreate,
Read: resourceCVOSnapMirrorRead,
Delete: resourceCVOSnapMirrorDelete,
Exists: resourceCVOSnapMirrorExists,
Update: resourceCVOSnapMirrorUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"source_working_environment_id": {
Type: schema.TypeString,
Optional: true,
},
"destination_working_environment_id": {
Type: schema.TypeString,
Optional: true,
},
"source_working_environment_name": {
Type: schema.TypeString,
Optional: true,
},
"destination_working_environment_name": {
Type: schema.TypeString,
Optional: true,
},
"destination_aggregate_name": {
Type: schema.TypeString,
Optional: true,
},
"policy": {
Type: schema.TypeString,
Optional: true,
Default: "MirrorAllSnapshots",
},
"schedule": {
Type: schema.TypeString,
Optional: true,
Default: "1hour",
},
"max_transfer_rate": {
Type: schema.TypeInt,
Optional: true,
Default: 100000,
},
"source_svm_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"destination_svm_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"source_volume_name": {
Type: schema.TypeString,
Required: true,
},
"destination_volume_name": {
Type: schema.TypeString,
Required: true,
},
"provider_volume_type": {
Type: schema.TypeString,
Optional: true,
},
"capacity_tier": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"S3", "Blob", "cloudStorage", "none"}, false),
},
"client_id": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func resourceCVOSnapMirrorCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("Creating SnapMirror: %#v", d)

client := meta.(*Client)
client.ClientID = d.Get("client_id").(string)
snapMirror := snapMirrorRequest{}

sourceWEInfo, destWEInfo, err := client.getWorkingEnvironmentDetailForSnapMirror(d)
if err != nil {
log.Print("Cannot find working environment")
return err
}

snapMirror.ReplicationRequest.SourceWorkingEnvironmentID = sourceWEInfo.PublicID
snapMirror.ReplicationRequest.DestinationWorkingEnvironmentID = destWEInfo.PublicID

snapMirror.ReplicationVolume.SourceVolumeName = d.Get("source_volume_name").(string)
snapMirror.ReplicationVolume.DestinationVolumeName = d.Get("destination_volume_name").(string)
snapMirror.ReplicationRequest.PolicyName = d.Get("policy").(string)
snapMirror.ReplicationRequest.ScheduleName = d.Get("schedule").(string)
snapMirror.ReplicationRequest.MaxTransferRate = d.Get("max_transfer_rate").(int)

if s, ok := d.GetOk("destination_aggregate_name"); ok {
snapMirror.ReplicationVolume.DestinationAggregateName = s.(string)
}
if s, ok := d.GetOk("source_svm_name"); ok {
snapMirror.ReplicationVolume.SourceSvmName = s.(string)
}
if s, ok := d.GetOk("destination_svm_name"); ok {
snapMirror.ReplicationVolume.DestinationSvmName = s.(string)
}
if s, ok := d.GetOk("provider_volume_type"); ok {
snapMirror.ReplicationVolume.DestinationProviderVolumeType = s.(string)
}
if s, ok := d.GetOk("capacity_tier"); ok {
snapMirror.ReplicationVolume.DestinationCapacityTier = s.(string)
}
if snapMirror.ReplicationVolume.DestinationCapacityTier == "" {
cloudProvider := strings.ToLower(destWEInfo.CloudProviderName)
if cloudProvider == "aws" {
snapMirror.ReplicationVolume.DestinationCapacityTier = "S3"
} else if cloudProvider == "azure" {
snapMirror.ReplicationVolume.DestinationCapacityTier = "Blob"
} else if cloudProvider == "gcp" {
snapMirror.ReplicationVolume.DestinationCapacityTier = "cloudStorage"
}
}

if snapMirror.ReplicationVolume.SourceSvmName == "" {
snapMirror.ReplicationVolume.SourceSvmName = sourceWEInfo.SvmName
}
if snapMirror.ReplicationVolume.DestinationSvmName == "" {
snapMirror.ReplicationVolume.DestinationSvmName = destWEInfo.SvmName
}

res, err := client.buildSnapMirrorCreate(snapMirror)
if err != nil {
log.Print("Error creating SnapMirrorCreate")
return err
}

d.SetId(res.ReplicationVolume.DestinationVolumeName)

d.Set("source_svm_name", res.ReplicationVolume.SourceSvmName)
d.Set("destination_svm_name", res.ReplicationVolume.DestinationSvmName)
d.Set("source_volume_name", res.ReplicationVolume.SourceVolumeName)
d.Set("destination_volume_name", res.ReplicationVolume.DestinationVolumeName)

return resourceCVOSnapMirrorRead(d, meta)
}

func resourceCVOSnapMirrorRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Fetching SnapMirror: %#v", d)

client := meta.(*Client)
client.ClientID = d.Get("client_id").(string)

snapMirror := snapMirrorRequest{}

sourceWEInfo, destWEInfo, err := client.getWorkingEnvironmentDetailForSnapMirror(d)
if err != nil {
log.Print("Cannot find working environment")
return err
}

snapMirror.ReplicationRequest.SourceWorkingEnvironmentID = sourceWEInfo.PublicID
snapMirror.ReplicationRequest.DestinationWorkingEnvironmentID = destWEInfo.PublicID
snapMirror.ReplicationVolume.SourceVolumeName = d.Get("source_volume_name").(string)
snapMirror.ReplicationVolume.DestinationVolumeName = d.Get("destination_volume_name").(string)
_, err = client.getSnapMirror(snapMirror, d.Id())
if err != nil {
log.Print("Error getting SnapMirror")
return err
}

return nil
}

func resourceCVOSnapMirrorDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("Deleting SnapMirror: %#v", d)
client := meta.(*Client)
client.ClientID = d.Get("client_id").(string)
snapMirror := snapMirrorRequest{}

sourceWEInfo, destWEInfo, err := client.getWorkingEnvironmentDetailForSnapMirror(d)
if err != nil {
log.Print("Cannot find working environment")
return err
}

snapMirror.ReplicationRequest.SourceWorkingEnvironmentID = sourceWEInfo.PublicID
snapMirror.ReplicationRequest.DestinationWorkingEnvironmentID = destWEInfo.PublicID
snapMirror.ReplicationVolume.DestinationSvmName = d.Get("destination_svm_name").(string)
snapMirror.ReplicationVolume.SourceVolumeName = d.Get("source_volume_name").(string)
snapMirror.ReplicationVolume.DestinationVolumeName = d.Get("destination_volume_name").(string)
if snapMirror.ReplicationVolume.DestinationVolumeName == "" {
snapMirror.ReplicationVolume.DestinationVolumeName = snapMirror.ReplicationVolume.SourceVolumeName + "_copy"
}
if s, ok := d.GetOk("source_svm_name"); ok {
snapMirror.ReplicationVolume.SourceSvmName = s.(string)
}

err = client.deleteSnapMirror(snapMirror)
if err != nil {
log.Print("Error deleting SnapMirror")
return err
}
return nil
}

func resourceCVOSnapMirrorExists(d *schema.ResourceData, meta interface{}) (bool, error) {
log.Printf("Checking existence of SnapMirror: %#v", d)
client := meta.(*Client)
client.ClientID = d.Get("client_id").(string)
snapMirror := snapMirrorRequest{}

sourceWEInfo, destWEInfo, err := client.getWorkingEnvironmentDetailForSnapMirror(d)
if err != nil {
log.Print("Cannot find working environment")
return false, err
}

snapMirror.ReplicationRequest.SourceWorkingEnvironmentID = sourceWEInfo.PublicID
snapMirror.ReplicationRequest.DestinationWorkingEnvironmentID = destWEInfo.PublicID
snapMirror.ReplicationVolume.SourceVolumeName = d.Get("source_volume_name").(string)
snapMirror.ReplicationVolume.DestinationVolumeName = d.Get("destination_volume_name").(string)
snapMirror.ReplicationVolume.DestinationSvmName = d.Get("destination_svm_name").(string)
res, err := client.getSnapMirror(snapMirror, d.Id())
if err != nil {
log.Print("Error getting SnapMirror")
return false, err
}

if res != d.Id() {
d.SetId("")
return false, nil
}
return true, nil
}

func resourceCVOSnapMirrorUpdate(d *schema.ResourceData, meta interface{}) error {

return nil
}
Loading

0 comments on commit 239ce86

Please sign in to comment.