Skip to content

Commit

Permalink
Sync bitbucket and GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
carchi8py committed Aug 18, 2023
1 parent 1a4924b commit 2f433e5
Show file tree
Hide file tree
Showing 6 changed files with 520 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/resources/netapp-ontap_snapmiror/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
terraform {
required_providers {
netapp-ontap = {
source = "NetApp/netapp-ontap"
version = "0.0.1"
}
}
}


provider "netapp-ontap" {
# A connection profile defines how to interface with an ONTAP cluster or svm.
# At least one is required.
connection_profiles = [
{
name = "cluster1"
hostname = "********219"
username = var.username
password = var.password
validate_certs = var.validate_certs
},
{
name = "cluster2"
hostname = "********222"
username = var.username
password = var.password
validate_certs = var.validate_certs
},
{
name = "cluster3"
hostname = "10.193.176.159"
username = var.username
password = var.password
validate_certs = var.validate_certs
},
{
name = "cluster4"
hostname = "10.193.180.108"
username = var.username
password = var.password
validate_certs = var.validate_certs
}
]
}
10 changes: 10 additions & 0 deletions examples/resources/netapp-ontap_snapmiror/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "netapp-ontap_snapmirror_resource" "snapmirror_async" {
# required to know which system to interface with
cx_profile_name = "cluster1"
source_endpoint = {
path = "snapmirror_source_svm:snap"
}
destination_endpoint = {
path = "snapmirror_dest_svm:snap_dest"
}
}
11 changes: 11 additions & 0 deletions examples/resources/netapp-ontap_snapmiror/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Terraform will prompt for values, unless a tfvars file is present.
variable "username" {
type = string
}
variable "password" {
type = string
sensitive = true
}
variable "validate_certs" {
type = bool
}
111 changes: 111 additions & 0 deletions internal/interfaces/snapmirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package interfaces

import (
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/mapstructure"
"github.com/netapp/terraform-provider-netapp-ontap/internal/restclient"
"github.com/netapp/terraform-provider-netapp-ontap/internal/utils"
)

// SnapmirrorGetDataModelONTAP defines the resource get data model
type SnapmirrorGetDataModelONTAP struct {
Healthy bool `mapstructure:"healthy"`
State string `mapstructure:"state"`
UUID string `mapstructure:"uuid"`
}

// SnapmirrorGetRawDataModelONTAP defines the resource get data model
type SnapmirrorGetRawDataModelONTAP struct {
UUID string `mapstructure:"uuid"`
}

// SnapmirrorResourceBodyDataModelONTAP defines the resource data model
type SnapmirrorResourceBodyDataModelONTAP struct {
SourceEndPoint EndPoint `mapstructure:"source"`
DestinationEndPoint EndPoint `mapstructure:"destination"`
CreateDestination CreateDestination `mapstructure:"create_destination,omitempty"`
}

// EndPoint defines source/destination endpoint data model.
type EndPoint struct {
Cluster Cluster `mapstructure:"cluster,omitempty"`
Path string `mapstructure:"path"`
}

// CreateDestination defines CreateDestination data model.
type CreateDestination struct {
Enabled bool `mapstructure:"enabled"`
}

// Cluster defines Cluster data model.
type Cluster struct {
Name string `mapstructure:"name,omitempty"`
}

// GetSnapmirrorByID ...
func GetSnapmirrorByID(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string) (*SnapmirrorGetDataModelONTAP, error) {
api := "snapmirror/relationships/" + id
statusCode, response, err := r.GetNilOrOneRecord(api, nil, nil)
if err == nil && response == nil {
err = fmt.Errorf("no response for GET %s", api)
}
if err != nil {
return nil, errorHandler.MakeAndReportError("error reading snapmirror info", fmt.Sprintf("error on GET %s: %s", api, err))
}
var rawDataONTAP SnapmirrorGetDataModelONTAP
if err := mapstructure.Decode(response, &rawDataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding snapmirror info", fmt.Sprintf("error on decode %s: %s, statusCode %d, response %#v", api, err, statusCode, response))
}
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read snapmirror source - udata: %#v", rawDataONTAP))
return &rawDataONTAP, nil
}

// CreateSnapmirror to create snapmirror
func CreateSnapmirror(errorHandler *utils.ErrorHandler, r restclient.RestClient, body SnapmirrorResourceBodyDataModelONTAP) (*SnapmirrorGetRawDataModelONTAP, error) {
api := "snapmirror/relationships"
var bodyMap map[string]interface{}
if err := mapstructure.Decode(body, &bodyMap); err != nil {
return nil, errorHandler.MakeAndReportError("error encoding snapmirror/relationships body", fmt.Sprintf("error on encoding %s body: %s, body: %#v", api, err, body))
}
// tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read vserver info: %#v", bodyMap))
query := r.NewQuery()
query.Add("return_records", "true")
statusCode, response, err := r.CallCreateMethod(api, query, bodyMap)
if err != nil {
return nil, errorHandler.MakeAndReportError("error creating snapmirror", fmt.Sprintf("error on POST %s: %s, statusCode %d", api, err, statusCode))
}

var rawDataONTAP SnapmirrorGetRawDataModelONTAP
if err := mapstructure.Decode(response.Records[0], &rawDataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding snapmirror info", fmt.Sprintf("error on decode snapmirror info: %s, statusCode %d, response %#v", err, statusCode, response))
}

tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Create snapmirror source - udata: %#v", rawDataONTAP))
return &rawDataONTAP, nil
}

// InitializeSnapmirror ...
func InitializeSnapmirror(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string, state string) error {
api := "snapmirror/relationships/" + id
body := map[string]interface{}{"state": state}
query := r.NewQuery()
query.Add("return_records", "true")
statusCode, response, err := r.CallUpdateMethod(api, query, body)
if err != nil {
return errorHandler.MakeAndReportError("error initializing snapmirror", fmt.Sprintf("error on PATCH %s: %s, statusCode %d, response %#v", api, err, statusCode, response))
}

return nil
}

// DeleteSnapmirror to delete ip_interface
func DeleteSnapmirror(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string) error {
api := "snapmirror/relationships/" + id
statusCode, _, err := r.CallDeleteMethod(api, nil, nil)
if err != nil {
return errorHandler.MakeAndReportError("error deleting snapmirror/relationships", fmt.Sprintf("error on DELETE %s: %s, statusCode %d", api, err, statusCode))
}
return nil
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (p *ONTAPProvider) Resources(ctx context.Context) []func() resource.Resourc
NewIPRouteResource,
NewNameServicesDNSResource,
NewProtocolsNfsServiceResource,
NewSnapmirrorResource,
NewSnapmirrorPolicyResource,
NewSnapshotPolicyResource,
NewStorageVolumeResource,
Expand Down
Loading

0 comments on commit 2f433e5

Please sign in to comment.