From 7321185ba33ea8d1bdd23b482b247a6457eb14bb Mon Sep 17 00:00:00 2001 From: Ryan Johnson Date: Wed, 29 Jan 2025 09:26:15 -0500 Subject: [PATCH] fix: ensure mutual exclusivity - Adds use of `ConflictsWith` for `r/hcx_vmc` for `sddc_id` and `sddc_name` to ensure mutual exclusivity. - Adds use of `ExactlyOneO` for `r/hcx_vmc` for `sddc_id` and `sddc_name` to ensure at least one of them is provided. Signed-off-by: Ryan Johnson --- docs/resources/vmc.md | 8 ++++---- resource_vmc.go | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/resources/vmc.md b/docs/resources/vmc.md index 93ba006..51f7f5c 100644 --- a/docs/resources/vmc.md +++ b/docs/resources/vmc.md @@ -24,10 +24,10 @@ resource "hcx_site_pairing" "example" { ## Argument Reference -* `sddc_name` - (Optional) The name of the SDDC. Either `sddc_name` or `sddc_id` - must be specified. -* `sddc_id` - (Optional) The ID of the SDDC. Either `sddc_id` or `sddc_name` - must be specified. +* `sddc_name` - (Optional) Specifies the name of the SDDC. +* `sddc_id` - (Optional) Specifies the ID of the SDDC. + +~> **NOTE:** Either `sddc_name` or `sddc_id` **must** be provided, but not both. ## Attribute Reference diff --git a/resource_vmc.go b/resource_vmc.go index 61d81e4..0867898 100644 --- a/resource_vmc.go +++ b/resource_vmc.go @@ -25,14 +25,18 @@ func resourceVmc() *schema.Resource { Schema: map[string]*schema.Schema{ "sddc_id": { - Type: schema.TypeString, - Description: "The ID of the SDDC.", - Optional: true, + Type: schema.TypeString, + Description: "The ID of the SDDC.", + Optional: true, + ConflictsWith: []string{"sddc_name"}, // Ensures mutual exclusivity. + ExactlyOneOf: []string{"sddc_id", "sddc_name"}, // Enforces that at least one of them is provided. }, "sddc_name": { - Type: schema.TypeString, - Description: "The name of the SDDC.", - Optional: true, + Type: schema.TypeString, + Description: "The name of the SDDC.", + Optional: true, + ConflictsWith: []string{"sddc_id"}, // Ensures mutual exclusivity. + ExactlyOneOf: []string{"sddc_id", "sddc_name"}, // Enforces that at least one of them is provided. }, "cloud_url": { Type: schema.TypeString, @@ -62,10 +66,6 @@ func resourceVmcCreate(ctx context.Context, d *schema.ResourceData, m interface{ sddcName := d.Get("sddc_name").(string) sddcID := d.Get("sddc_id").(string) - if sddcName == "" && sddcID == "" { - return diag.Errorf("SDDC name or Id must be specified") - } - // Authenticate with VMware Cloud Services accessToken, err := hcx.VmcAuthenticate(token) if err != nil {