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 Jun 29, 2021
1 parent 4736ee8 commit 64a2ac0
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 61 deletions.
23 changes: 1 addition & 22 deletions cloudmanager/cvo_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/fatih/structs"
"github.com/hashicorp/terraform/helper/schema"
)

// AWSLicenseTypes is the AWS License types
Expand Down Expand Up @@ -40,7 +39,7 @@ type createCVOAWSDetails struct {
EnableCompliance bool `structs:"enableCompliance"`
EnableMonitoring bool `structs:"enableMonitoring"`
AwsEncryptionParameters awsEncryptionParameters `structs:"awsEncryptionParameters,omitempty"`
AwsTags []awsTags `structs:"awsTags,omitempty"`
AwsTags []userTags `structs:"awsTags,omitempty"`
IsHA bool
HAParams haParamsAWS `structs:"haParams,omitempty"`
}
Expand Down Expand Up @@ -82,12 +81,6 @@ type awsEncryptionParameters struct {
KmsKeyID string `structs:"kmsKeyId,omitempty"`
}

// awsTags the input for requesting a CVO
type awsTags struct {
TagKey string `structs:"tagKey"`
TagValue string `structs:"tagValue,omitempty"`
}

// deleteCVODetails the users input for deleting a cvo
type deleteCVODetails struct {
InstanceID string
Expand Down Expand Up @@ -347,20 +340,6 @@ func (c *Client) deleteCVO(id string, isHA bool) error {
return nil
}

// expandAWSTags converts set to awsEncryptionParameters struct
func expandAWSTags(set *schema.Set) []awsTags {
tags := []awsTags{}

for _, v := range set.List() {
tag := v.(map[string]interface{})
awsTag := awsTags{}
awsTag.TagKey = tag["tag_key"].(string)
awsTag.TagValue = tag["tag_value"].(string)
tags = append(tags, awsTag)
}
return tags
}

// validateCVOParams validates params
func validateCVOParams(cvoDetails createCVOAWSDetails) error {
if cvoDetails.VsaMetadata.UseLatestVersion == true && cvoDetails.VsaMetadata.OntapVersion != "latest" {
Expand Down
25 changes: 2 additions & 23 deletions cloudmanager/cvo_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/fatih/structs"
"github.com/hashicorp/terraform/helper/schema"
)

// AzureLicenseTypes is the Azure License types
Expand Down Expand Up @@ -38,9 +37,9 @@ type createCVOAzureDetails struct {
EnableCompliance bool `structs:"enableCompliance"`
EnableMonitoring bool `structs:"enableMonitoring"`
AllowDeployInExistingRg bool `structs:"allowDeployInExistingRg,omitempty"`
AzureTags []azureTags `structs:"azureTags,omitempty"`
AzureTags []userTags `structs:"azureTags,omitempty"`
IsHA bool
ResourceGroup string `structs:"resourceGroup,omitempty"`
ResourceGroup string `structs:"resourceGroup,omitempty"`
VnetResourceGroup string
VnetForInternal string
SerialNumber string `structs:"serialNumber,omitempty"`
Expand All @@ -53,12 +52,6 @@ type haParamsAzure struct {
PlatformSerialNumberNode2 string `structs:"platformSerialNumberNode2,omitempty"`
}

// azureTags the input for requesting a CVO
type azureTags struct {
TagKey string `structs:"tagKey"`
TagValue string `structs:"tagValue,omitempty"`
}

// cvoListAzure the users input for getting cvo
type cvoListAzure struct {
CVO []cvoResult `json:"azureVsaWorkingEnvironments"`
Expand Down Expand Up @@ -318,20 +311,6 @@ func (c *Client) deleteCVOAzure(id string, isHA bool) error {
return nil
}

// expandAzureTags converts set to azureTags struct
func expandAzureTags(set *schema.Set) []azureTags {
tags := []azureTags{}

for _, v := range set.List() {
tag := v.(map[string]interface{})
azureTag := azureTags{}
azureTag.TagKey = tag["tag_key"].(string)
azureTag.TagValue = tag["tag_value"].(string)
tags = append(tags, azureTag)
}
return tags
}

// validateCVOParams validates params
func validateCVOAzureParams(cvoDetails createCVOAzureDetails) error {
if cvoDetails.VsaMetadata.UseLatestVersion == true && cvoDetails.VsaMetadata.OntapVersion != "latest" {
Expand Down
14 changes: 14 additions & 0 deletions cloudmanager/cvo_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ func expandGCPLabels(set *schema.Set) []gcpLabels {
return labels
}

// expandGCPLabelsToUseTags
func expandGCPLabelsToUserTags(set *schema.Set) []userTags {
tags := []userTags{}

for _, v := range set.List() {
label := v.(map[string]interface{})
userTag := userTags{}
userTag.TagKey = label["label_key"].(string)
userTag.TagValue = label["label_value"].(string)
tags = append(tags, userTag)
}
return tags
}

// validateCVOGCPParams validates params
func validateCVOGCPParams(cvoDetails createCVOGCPDetails) error {
if cvoDetails.VsaMetadata.UseLatestVersion == true && cvoDetails.VsaMetadata.OntapVersion != "latest" {
Expand Down
114 changes: 114 additions & 0 deletions cloudmanager/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/fatih/structs"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -32,6 +33,17 @@ type workingEnvironmentResult struct {
GcpVsaWorkingEnvironments []workingEnvironmentInfo `json:"gcpVsaWorkingEnvironments"`
}

// userTags the input for requesting a CVO
type userTags struct {
TagKey string `structs:"tagKey"`
TagValue string `structs:"tagValue,omitempty"`
}

// ModifyUserTagsRequest the input for requesting tags modificaiton
type modifyUserTagsRequest struct {
Tags []userTags `structs:"tags"`
}

// Check HTTP response code, return error if HTTP request is not successed.
func apiResponseChecker(statusCode int, response []byte, funcName string) error {

Expand Down Expand Up @@ -413,3 +425,105 @@ func (c *Client) findWorkingEnvironmentForID(id string) (workingEnvironmentInfo,

return workingEnvironmentInfo{}, err
}

// customized check diff user-tags (aws_tag, azure_tag, gcp_label)
func checkUserTagDiff(diff *schema.ResourceDiff, tagName string, keyName string) error {
if diff.HasChange(tagName) {
_, expectTag := diff.GetChange(tagName)
etags := expectTag.(*schema.Set)
if etags.Len() > 0 {
log.Println("etags len: ", etags.Len())
// check each of the tag_key in the list is unique
respErr := checkUserTagKeyUnique(etags, keyName)
if respErr != nil {
return respErr
}
}
}
return nil
}

// check each of the tag_key or label_key in the list is unique
func checkUserTagKeyUnique(etags *schema.Set, keyName string) error {
m := make(map[string]bool)
for _, v := range etags.List() {
tag := v.(map[string]interface{})
tkey := tag[keyName].(string)
if _, ok := m[tkey]; !ok {
m[tkey] = true
} else {
return fmt.Errorf("Tag key %s is not unique", tkey)
}
}
return nil
}

// expandUserTags converts set to userTags struct
func expandUserTags(set *schema.Set) []userTags {
tags := []userTags{}

for _, v := range set.List() {
tag := v.(map[string]interface{})
userTag := userTags{}
userTag.TagKey = tag["tag_key"].(string)
userTag.TagValue = tag["tag_value"].(string)
tags = append(tags, userTag)
}
return tags
}

// modify the CVO user-tags
func (c *Client) callUpdateUserTags(request modifyUserTagsRequest, id string, isHA bool) error {
apiRoot, _, err := c.getAPIRoot(id)
baseURL := fmt.Sprintf("%s/working-environments/%s/user-tags", apiRoot, id)

hostType := "CloudManagerHost"
params := structs.Map(request)

accessTokenResult, err := c.getAccessToken()
if err != nil {
log.Print("in updateCVOUserTags request, failed to get AccessToken")
return err
}
c.Token = accessTokenResult.Token

statusCode, response, _, err := c.CallAPIMethod("PUT", baseURL, params, c.Token, hostType)
if err != nil {
log.Print("updateCVOUserTags request failed: ", statusCode)
log.Print("call api response: ", response)
return err
}

responseError := apiResponseChecker(statusCode, response, "updateCVOUserTags")
if responseError != nil {
return responseError
}
return nil
}

// update CVO user-tags
func updateCVOUserTags(d *schema.ResourceData, meta interface{}, tagName string) error {
client := meta.(*Client)
client.ClientID = d.Get("client_id").(string)
var request modifyUserTagsRequest
if c, ok := d.GetOk(tagName); ok {
tags := c.(*schema.Set)
if tags.Len() > 0 {
if tagName == "gcp_label" {
request.Tags = expandGCPLabelsToUserTags(tags)
} else {
request.Tags = expandUserTags(tags)
}
log.Print("Update user-tags: ", request.Tags)
}
}
// Update tags
isHA := d.Get("is_ha").(bool)
id := d.Id()
updateErr := client.callUpdateUserTags(request, id, isHA)
if updateErr != nil {
return updateErr
}
log.Printf("Updated %s %s: %v", id, tagName, request.Tags)
return nil
}
2 changes: 1 addition & 1 deletion cloudmanager/occm_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type createOCCMDetails struct {
AssociatePublicIP bool
FirewallTags bool
EnableTerminationProtection *bool
AwsTags []awsTags
AwsTags []userTags
}

// deleteOCCMDetails the users input for deleting a occm
Expand Down
2 changes: 1 addition & 1 deletion cloudmanager/resource_netapp_cloudmanager_connector_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func resourceOCCMAWSCreate(d *schema.ResourceData, meta interface{}) error {
if o, ok := d.GetOk("aws_tag"); ok {
tags := o.(*schema.Set)
if tags.Len() > 0 {
occmDetails.AwsTags = expandAWSTags(tags)
occmDetails.AwsTags = expandUserTags(tags)
}
}

Expand Down
30 changes: 25 additions & 5 deletions cloudmanager/resource_netapp_cloudmanager_cvo_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ func resourceCVOAWS() *schema.Resource {
Create: resourceCVOAWSCreate,
Read: resourceCVOAWSRead,
Delete: resourceCVOAWSDelete,
Update: resourceCVOAWSUpdate,
Exists: resourceCVOAWSExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

CustomizeDiff: resourceCVOAWSCustomizeDiff,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -195,18 +196,15 @@ func resourceCVOAWS() *schema.Resource {
"aws_tag": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tag_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tag_value": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
Expand Down Expand Up @@ -322,7 +320,7 @@ func resourceCVOAWSCreate(d *schema.ResourceData, meta interface{}) error {
if c, ok := d.GetOk("aws_tag"); ok {
tags := c.(*schema.Set)
if tags.Len() > 0 {
cvoDetails.AwsTags = expandAWSTags(tags)
cvoDetails.AwsTags = expandUserTags(tags)
}
}
cvoDetails.EbsVolumeSize.Size = d.Get("ebs_volume_size").(int)
Expand Down Expand Up @@ -470,6 +468,28 @@ func resourceCVOAWSDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceCVOAWSUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("Updating CVO: %#v", d)

// check if aws_tag has changes
if d.HasChange("aws_tag") {
respErr := updateCVOUserTags(d, meta, "aws_tag")
if respErr != nil {
return respErr
}
return resourceCVOAWSRead(d, meta)
}
return nil
}

func resourceCVOAWSCustomizeDiff(diff *schema.ResourceDiff, v interface{}) error {
respErr := checkUserTagDiff(diff, "aws_tag", "tag_key")
if respErr != nil {
return respErr
}
return nil
}

func resourceCVOAWSExists(d *schema.ResourceData, meta interface{}) (bool, error) {
log.Printf("Checking existence of CVO: %#v", d)
client := meta.(*Client)
Expand Down
Loading

0 comments on commit 64a2ac0

Please sign in to comment.