Skip to content

Commit

Permalink
PR
Browse files Browse the repository at this point in the history
Signed-off-by: Dainius Serplis <[email protected]>
  • Loading branch information
Didainius committed Feb 27, 2022
1 parent 9fa516f commit 93cc7ce
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 127 deletions.
2 changes: 2 additions & 0 deletions .changes/v3.6.0/793-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* `resource/vcd_nsxt_edgegateway` and `resource/vcd_nsxt_edgegateway` support VDC groups via new
field `owner_id` replacing `vdc` [GH-793]
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# These owners will be the default owners for everything in the repo. Unless a later match takes
# precedence all these users will be requested for review when someone opens a pull request.
* @lvirbalas @dataclouder @Didainius @vbauzysvmware
* @lvirbalas @dataclouder @Didainius @vbauzysvmware @adambarreiro @mikeletux
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ require (
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.2
)

//replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220224195422-2a5f827ef552

//replace github.com/vmware/go-vcloud-director/v2 => ../go-vcloud-director
//replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.12.1-0.20211018060826-c7f8ab32330e
replace github.com/vmware/go-vcloud-director/v2 => ../go-vcloud-director
replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220226193642-991c1925558d
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220226193642-991c1925558d h1:hNMNmiXDXZVzREppZv/r0dBBY89o5RU+7LReKnZLyvk=
github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220226193642-991c1925558d/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
Expand Down
116 changes: 95 additions & 21 deletions vcd/datasource_vcd_nsxt_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,43 @@ func datasourceVcdNsxtEdgeGateway() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdNsxtEdgeGatewayRead,
Schema: map[string]*schema.Schema{
"org": &schema.Schema{
"org": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of organization to use, optional if defined at provider " +
"level. Useful when connected as sysadmin working across different organizations",
},
"vdc": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of VDC to use, optional if defined at provider level",
"vdc": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of VDC to use, optional if defined at provider level",
ConflictsWith: []string{"owner_id"},
},
"owner_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "ID of VDC group (if applicable)",
"owner_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "ID of VDC group (if applicable)",
ConflictsWith: []string{"vdc"},
},
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Edge Gateway name",
},
"description": &schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Edge Gateway description",
},
"dedicate_external_network": &schema.Schema{
"dedicate_external_network": {
Type: schema.TypeBool,
Computed: true,
Description: "Dedicating the External Network will enable Route Advertisement for this Edge Gateway.",
},
"external_network_id": &schema.Schema{
"external_network_id": {
Type: schema.TypeString,
Computed: true,
Description: "External network ID",
Expand Down Expand Up @@ -100,7 +102,7 @@ func datasourceVcdNsxtEdgeGateway() *schema.Resource {
Computed: true,
Description: "Primary IP address of edge gateway",
},
"edge_cluster_id": &schema.Schema{
"edge_cluster_id": {
Type: schema.TypeString,
Optional: true,
Description: "NSX-T Edge Cluster ID.",
Expand All @@ -118,12 +120,27 @@ func datasourceVcdNsxtEdgeGatewayRead(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(fmt.Errorf("error retrieving Org: %s", err))
}

//if vdc.IsNsxv() {
// return diag.Errorf("please use 'vcd_edgegateway' for NSX-V backed VDC")
//}
// Validate if VDC or VDC Group is NSX-T backed
inheritedVdcField := vcdClient.Vdc
vdcField := d.Get("vdc").(string)
ownerIdField := d.Get("owner_id").(string)
usedFieldId, err := pickVdcIdByPriority(org, inheritedVdcField, vdcField, ownerIdField)

var edge *govcd.NsxtEdgeGateway
if err != nil {
return diag.Errorf("error finding VDC ID: %s", err)
}

isNsxt, err := isBackedByNsxt(org, usedFieldId)
if err != nil {
return diag.Errorf("error ")
}

if !isNsxt {
return diag.Errorf("please use 'vcd_edgegateway' for NSX-V backed VDC")
}
// EOF validate if VDC or VDC Group is NSX-T backed

var edge *govcd.NsxtEdgeGateway
edgeGatewayName := d.Get("name").(string)
ownerId := d.Get("owner_id").(string)
switch {
Expand All @@ -141,10 +158,67 @@ func datasourceVcdNsxtEdgeGatewayRead(ctx context.Context, d *schema.ResourceDat

err = setNsxtEdgeGatewayData(edge.EdgeGateway, d)
if err != nil {
return diag.FromErr(fmt.Errorf("error reading NSX-T edge gateway data: %s", err))
return diag.FromErr(fmt.Errorf("error reading NSX-T Edge Gateway data: %s", err))
}

d.SetId(edge.EdgeGateway.ID)

return nil
}

// pickVdcIdByPriority picks primary field to be used from the specified ones. The priority is such
// * `owner_id`
// * `vdc` at resource level
// * `vdc` inherited from provider configuration
func pickVdcIdByPriority(org *govcd.Org, inheritedVdcField, vdcField, ownerIdField string) (string, error) {
if ownerIdField != "" {
return ownerIdField, nil
}

if vdcField != "" {
vdc, err := org.GetVDCByName(vdcField, false)
if err != nil {
return "", fmt.Errorf("error finding VDC '%s': %s", vdc.Vdc.ID, err)
}
return vdc.Vdc.ID, nil
}

if inheritedVdcField != "" {
vdc, err := org.GetVDCByName(inheritedVdcField, false)
if err != nil {
return "", fmt.Errorf("error finding VDC '%s': %s", vdc.Vdc.ID, err)
}
return vdc.Vdc.ID, nil
}

return "", fmt.Errorf("none of the fields `owner_id`, `vdc` and provider inherited `vdc`")
}

// isBackedByNsxt accepts VDC or VDC Group ID and checks if it is backed by NSX-T
func isBackedByNsxt(org *govcd.Org, vdcOrVdcGroupId string) (bool, error) {
var vdcOrGroup vdcOrVdcGroupVerifier
var err error

switch {
case govcd.OwnerIsVdc(vdcOrVdcGroupId):
vdcOrGroup, err = org.GetVDCById(vdcOrVdcGroupId, false)
if err != nil {
return false, err
}
case govcd.OwnerIsVdcGroup(vdcOrVdcGroupId):
vdcOrGroup, err = org.GetVdcGroupById(vdcOrVdcGroupId)
if err != nil {
return false, err
}
default:
return false, fmt.Errorf("error determining VDC type by ID '%s'", vdcOrVdcGroupId)
}

return vdcOrGroup.IsNsxt(), nil
}

// vdcOrVdcGroupVerifier is an interface to access IsNsxt() on VDC or VDC Group method `IsNsxt`
// (used in isBackedByNsxt)
type vdcOrVdcGroupVerifier interface {
IsNsxt() bool
}
8 changes: 5 additions & 3 deletions vcd/resource_vcd_catalog_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package vcd
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)

func resourceVcdCatalogItem() *schema.Resource {
Expand Down Expand Up @@ -289,14 +291,14 @@ func createOrUpdateCatalogItemMetadata(d *schema.ResourceData, meta interface{})
}
}
for _, k := range toBeRemovedMetadata {
err := vAppTemplate.DeleteMetadata(k)
err := vAppTemplate.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata: %s", err)
}
}
// Add new metadata
for k, v := range newMetadata {
_, err := vAppTemplate.AddMetadata(k, v.(string))
err := vAppTemplate.AddMetadataEntry(types.MetadataStringValue, k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata: %s", err)
}
Expand Down
8 changes: 5 additions & 3 deletions vcd/resource_vcd_catalog_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package vcd
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)

func resourceVcdCatalogMedia() *schema.Resource {
Expand Down Expand Up @@ -315,14 +317,14 @@ func createOrUpdateMediaItemMetadata(d *schema.ResourceData, meta interface{}) e
}
}
for _, k := range toBeRemovedMetadata {
err := media.DeleteMetadata(k)
err := media.DeleteMetadataEntry(k)
if err != nil {
return fmt.Errorf("error deleting metadata: %s", err)
}
}
// Add new metadata
for k, v := range newMetadata {
_, err = media.AddMetadata(k, v.(string))
err = media.AddMetadataEntry(types.MetadataStringValue, k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion vcd/resource_vcd_network_isolated_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func getOpenApiOrgVdcIsolatedNetworkType(d *schema.ResourceData, vdc *govcd.Vdc)
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
OrgVdc: &types.OpenApiReference{ID: vdc.Vdc.ID},
OwnerRef: &types.OpenApiReference{ID: vdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeIsolated,
Shared: takeBoolPointer(d.Get("is_shared").(bool)),
Expand Down
2 changes: 1 addition & 1 deletion vcd/resource_vcd_network_routed_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func getOpenApiOrgVdcNetworkType(d *schema.ResourceData, vdc *govcd.Vdc) (*types
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
OrgVdc: &types.OpenApiReference{ID: vdc.Vdc.ID},
OwnerRef: &types.OpenApiReference{ID: vdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeRouted,

Expand Down
20 changes: 10 additions & 10 deletions vcd/resource_vcd_nsxt_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ func getCreateOwnerId(d *schema.ResourceData, vcdClient *VCDClient, ownerIdField
log.Printf("[TRACE] NSX-T Edge Gateway create 'owner_id' field is set and is VDC group. 'starting_vdc_id' is not set. Choosing random starting VDC")

// Lookup Org
adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
org, err := vcdClient.GetOrgFromResource(d)
if err != nil {
return "", fmt.Errorf("error retrieving Org: %s", err)
}

vdcGroup, err := adminOrg.GetVdcGroupById(ownerIdField)
vdcGroup, err := org.GetVdcGroupById(ownerIdField)
if err != nil {
return "", fmt.Errorf("error retrieving VDC group: %s", err)
}
Expand All @@ -379,12 +379,12 @@ func getCreateOwnerId(d *schema.ResourceData, vcdClient *VCDClient, ownerIdField
case vdcField != "":
log.Printf("[TRACE] NSX-T Edge Gateway 'vdc' field is set in resource")

adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
org, err := vcdClient.GetOrgFromResource(d)
if err != nil {
return "", fmt.Errorf("error retrieving Org: %s", err)
}

vdc, err := adminOrg.GetVDCByName(vdcField, false)
vdc, err := org.GetVDCByName(vdcField, false)
if err != nil {
return "", fmt.Errorf("error finding VDC '%s': %s", vdcField, err)
}
Expand All @@ -394,12 +394,12 @@ func getCreateOwnerId(d *schema.ResourceData, vcdClient *VCDClient, ownerIdField
case inheritedVdcField != "" && vdcField == "" && ownerIdField == "":
log.Printf("[TRACE] NSX-T Edge Gateway 'vdc' field is inherited from provider configuration. `vdc` and `owner_id` are not set in resource.")

adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
org, err := vcdClient.GetOrgFromResource(d)
if err != nil {
return "", fmt.Errorf("error retrieving Org: %s", err)
}

vdc, err := adminOrg.GetVDCByName(inheritedVdcField, false)
vdc, err := org.GetVDCByName(inheritedVdcField, false)
if err != nil {
return "", fmt.Errorf("error finding VDC '%s': %s", inheritedVdcField, err)
}
Expand Down Expand Up @@ -432,12 +432,12 @@ func getUpdateOwnerId(d *schema.ResourceData, vcdClient *VCDClient, ownerIdField
case vdcField != "":
log.Printf("[TRACE] NSX-T Edge Gateway update 'vdc' field is set in resource")

adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
org, err := vcdClient.GetOrgFromResource(d)
if err != nil {
return "", fmt.Errorf("error retrieving Org: %s", err)
}

vdc, err := adminOrg.GetVDCByName(vdcField, false)
vdc, err := org.GetVDCByName(vdcField, false)
if err != nil {
return "", fmt.Errorf("error finding VDC '%s': %s", vdcField, err)
}
Expand All @@ -446,12 +446,12 @@ func getUpdateOwnerId(d *schema.ResourceData, vcdClient *VCDClient, ownerIdField
case inheritedVdcField != "" && vdcField == "" && ownerIdField == "":
log.Printf("[TRACE] NSX-T Edge Gateway update 'vdc' field is inherited from provider. `vdc` and `owner_id` are not set")

adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
org, err := vcdClient.GetOrgFromResource(d)
if err != nil {
return "", fmt.Errorf("error retrieving Org: %s", err)
}

vdc, err := adminOrg.GetVDCByName(inheritedVdcField, false)
vdc, err := org.GetVDCByName(inheritedVdcField, false)
if err != nil {
return "", fmt.Errorf("error finding VDC '%s': %s", inheritedVdcField, err)
}
Expand Down
Loading

0 comments on commit 93cc7ce

Please sign in to comment.