-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1464 from vmware/vpc-attachment-resource
Add VPC attachment resource
- Loading branch information
Showing
11 changed files
with
306 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. | ||
SPDX-License-Identifier: MPL-2.0 */ | ||
|
||
package nsxt | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"reflect" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client" | ||
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" | ||
clientLayer "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs" | ||
|
||
utl "github.com/vmware/terraform-provider-nsxt/api/utl" | ||
"github.com/vmware/terraform-provider-nsxt/nsxt/metadata" | ||
) | ||
|
||
var vpcAttachmentSchema = map[string]*metadata.ExtendedSchema{ | ||
"nsx_id": metadata.GetExtendedSchema(getNsxIDSchema()), | ||
"path": metadata.GetExtendedSchema(getPathSchema()), | ||
"display_name": metadata.GetExtendedSchema(getDisplayNameSchema()), | ||
"description": metadata.GetExtendedSchema(getDescriptionSchema()), | ||
"revision": metadata.GetExtendedSchema(getRevisionSchema()), | ||
"tag": metadata.GetExtendedSchema(getTagsSchema()), | ||
"parent_path": metadata.GetExtendedSchema(getPolicyPathSchema(true, true, "Policy path of the parent")), | ||
"vpc_connectivity_profile": { | ||
Schema: schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validatePolicyPath(), | ||
Required: true, | ||
}, | ||
Metadata: metadata.Metadata{ | ||
SchemaType: "string", | ||
SdkFieldName: "VpcConnectivityProfile", | ||
}, | ||
}, | ||
} | ||
|
||
func resourceNsxtVpcAttachment() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceNsxtVpcAttachmentCreate, | ||
Read: resourceNsxtVpcAttachmentRead, | ||
Update: resourceNsxtVpcAttachmentUpdate, | ||
Delete: resourceNsxtVpcAttachmentDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: nsxtParentPathResourceImporter, | ||
}, | ||
Schema: metadata.GetSchemaFromExtendedSchema(vpcAttachmentSchema), | ||
} | ||
} | ||
|
||
func resourceNsxtVpcAttachmentExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) { | ||
var err error | ||
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3) | ||
if pathErr != nil { | ||
return false, pathErr | ||
} | ||
client := clientLayer.NewAttachmentsClient(connector) | ||
_, err = client.Get(parents[0], parents[1], parents[2], id) | ||
if err == nil { | ||
return true, nil | ||
} | ||
|
||
if isNotFoundError(err) { | ||
return false, nil | ||
} | ||
|
||
return false, logAPIError("error retrieving resource", err) | ||
} | ||
|
||
func resourceNsxtVpcAttachmentCreate(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
id, err := getOrGenerateIDWithParent(d, m, resourceNsxtVpcAttachmentExists) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
parentPath := d.Get("parent_path").(string) | ||
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3) | ||
if pathErr != nil { | ||
return pathErr | ||
} | ||
displayName := d.Get("display_name").(string) | ||
description := d.Get("description").(string) | ||
tags := getPolicyTagsFromSchema(d) | ||
|
||
obj := model.VpcAttachment{ | ||
DisplayName: &displayName, | ||
Description: &description, | ||
Tags: tags, | ||
} | ||
|
||
elem := reflect.ValueOf(&obj).Elem() | ||
if err := metadata.SchemaToStruct(elem, d, vpcAttachmentSchema, "", nil); err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[INFO] Creating VpcAttachment with ID %s", id) | ||
|
||
client := clientLayer.NewAttachmentsClient(connector) | ||
err = client.Patch(parents[0], parents[1], parents[2], id, obj) | ||
if err != nil { | ||
return handleCreateError("VpcAttachment", id, err) | ||
} | ||
d.SetId(id) | ||
d.Set("nsx_id", id) | ||
|
||
return resourceNsxtVpcAttachmentRead(d, m) | ||
} | ||
|
||
func resourceNsxtVpcAttachmentRead(d *schema.ResourceData, m interface{}) error { | ||
connector := getPolicyConnector(m) | ||
|
||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("error obtaining VpcAttachment ID") | ||
} | ||
|
||
client := clientLayer.NewAttachmentsClient(connector) | ||
parentPath := d.Get("parent_path").(string) | ||
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3) | ||
if pathErr != nil { | ||
return pathErr | ||
} | ||
obj, err := client.Get(parents[0], parents[1], parents[2], id) | ||
if err != nil { | ||
return handleReadError(d, "VpcAttachment", id, err) | ||
} | ||
|
||
setPolicyTagsInSchema(d, obj.Tags) | ||
d.Set("nsx_id", id) | ||
d.Set("display_name", obj.DisplayName) | ||
d.Set("description", obj.Description) | ||
d.Set("revision", obj.Revision) | ||
d.Set("path", obj.Path) | ||
|
||
elem := reflect.ValueOf(&obj).Elem() | ||
return metadata.StructToSchema(elem, d, vpcAttachmentSchema, "", nil) | ||
} | ||
|
||
func resourceNsxtVpcAttachmentUpdate(d *schema.ResourceData, m interface{}) error { | ||
|
||
connector := getPolicyConnector(m) | ||
|
||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("error obtaining VpcAttachment ID") | ||
} | ||
|
||
parentPath := d.Get("parent_path").(string) | ||
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3) | ||
if pathErr != nil { | ||
return pathErr | ||
} | ||
description := d.Get("description").(string) | ||
displayName := d.Get("display_name").(string) | ||
tags := getPolicyTagsFromSchema(d) | ||
|
||
revision := int64(d.Get("revision").(int)) | ||
|
||
obj := model.VpcAttachment{ | ||
DisplayName: &displayName, | ||
Description: &description, | ||
Tags: tags, | ||
Revision: &revision, | ||
} | ||
|
||
elem := reflect.ValueOf(&obj).Elem() | ||
if err := metadata.SchemaToStruct(elem, d, vpcAttachmentSchema, "", nil); err != nil { | ||
return err | ||
} | ||
client := clientLayer.NewAttachmentsClient(connector) | ||
_, err := client.Update(parents[0], parents[1], parents[2], id, obj) | ||
if err != nil { | ||
return handleUpdateError("VpcAttachment", id, err) | ||
} | ||
|
||
return resourceNsxtVpcAttachmentRead(d, m) | ||
} | ||
|
||
func resourceNsxtVpcAttachmentDelete(d *schema.ResourceData, m interface{}) error { | ||
id := d.Id() | ||
if id == "" { | ||
return fmt.Errorf("error obtaining VpcAttachment ID") | ||
} | ||
|
||
connector := getPolicyConnector(m) | ||
parentPath := d.Get("parent_path").(string) | ||
|
||
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3) | ||
if pathErr != nil { | ||
return pathErr | ||
} | ||
|
||
client := clientLayer.NewAttachmentsClient(connector) | ||
err := client.Delete(parents[0], parents[1], parents[2], id) | ||
|
||
if err != nil { | ||
return handleDeleteError("VpcAttachment", id, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.