Skip to content

Commit

Permalink
Apply new labels model to more resources (part 1) (#8950) (#15877)
Browse files Browse the repository at this point in the history
* Apply new labels model to more resources (part 1)

* Handle labels field inside flatten object

* Fix the tests for datasource

* Fix typo

* Fix typo

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 15, 2023
1 parent 1a700a7 commit 23a2c59
Show file tree
Hide file tree
Showing 102 changed files with 2,118 additions and 676 deletions.
3 changes: 3 additions & 0 deletions .changelog/8950.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
6 changes: 6 additions & 0 deletions google/acctest/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc
if _, ok := ignoreFields[k]; ok {
continue
}
if _, ok := ignoreFields["labels.%"]; ok && strings.HasPrefix(k, "labels.") {
continue
}
if _, ok := ignoreFields["terraform_labels.%"]; ok && strings.HasPrefix(k, "terraform_labels.") {
continue
}
if k == "%" {
continue
}
Expand Down
96 changes: 73 additions & 23 deletions google/services/activedirectory/resource_active_directory_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func ResourceActiveDirectoryDomain() *schema.Resource {
},

CustomizeDiff: customdiff.All(
tpgresource.SetLabelsDiff,
tpgresource.DefaultProviderProject,
),

Expand Down Expand Up @@ -102,6 +103,12 @@ If CIDR subnets overlap between networks, domain creation will fail.`,
Description: `Resource labels that can contain user-provided metadata`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"effective_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"fqdn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -113,6 +120,13 @@ Similar to what would be chosen for an Active Directory set up on an internal ne
Computed: true,
Description: `The unique name of the domain using the format: 'projects/{project}/locations/global/domains/{domainName}'.`,
},
"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `The combination of labels configured directly on the resource
and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"project": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -132,12 +146,6 @@ func resourceActiveDirectoryDomainCreate(d *schema.ResourceData, meta interface{
}

obj := make(map[string]interface{})
labelsProp, err := expandActiveDirectoryDomainLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
authorizedNetworksProp, err := expandActiveDirectoryDomainAuthorizedNetworks(d.Get("authorized_networks"), d, config)
if err != nil {
return err
Expand All @@ -162,6 +170,12 @@ func resourceActiveDirectoryDomainCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("admin"); !tpgresource.IsEmptyValue(reflect.ValueOf(adminProp)) && (ok || !reflect.DeepEqual(v, adminProp)) {
obj["admin"] = adminProp
}
labelsProp, err := expandActiveDirectoryDomainEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{ActiveDirectoryBasePath}}projects/{{project}}/locations/global/domains?domainName={{domain_name}}")
if err != nil {
Expand Down Expand Up @@ -294,6 +308,12 @@ func resourceActiveDirectoryDomainRead(d *schema.ResourceData, meta interface{})
if err := d.Set("fqdn", flattenActiveDirectoryDomainFqdn(res["fqdn"], d, config)); err != nil {
return fmt.Errorf("Error reading Domain: %s", err)
}
if err := d.Set("terraform_labels", flattenActiveDirectoryDomainTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Domain: %s", err)
}
if err := d.Set("effective_labels", flattenActiveDirectoryDomainEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Domain: %s", err)
}

return nil
}
Expand All @@ -314,12 +334,6 @@ func resourceActiveDirectoryDomainUpdate(d *schema.ResourceData, meta interface{
billingProject = project

obj := make(map[string]interface{})
labelsProp, err := expandActiveDirectoryDomainLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
authorizedNetworksProp, err := expandActiveDirectoryDomainAuthorizedNetworks(d.Get("authorized_networks"), d, config)
if err != nil {
return err
Expand All @@ -332,6 +346,12 @@ func resourceActiveDirectoryDomainUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("locations"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, locationsProp)) {
obj["locations"] = locationsProp
}
labelsProp, err := expandActiveDirectoryDomainEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{ActiveDirectoryBasePath}}{{name}}")
if err != nil {
Expand All @@ -341,17 +361,17 @@ func resourceActiveDirectoryDomainUpdate(d *schema.ResourceData, meta interface{
log.Printf("[DEBUG] Updating Domain %q: %#v", d.Id(), obj)
updateMask := []string{}

if d.HasChange("labels") {
updateMask = append(updateMask, "labels")
}

if d.HasChange("authorized_networks") {
updateMask = append(updateMask, "authorizedNetworks")
}

if d.HasChange("locations") {
updateMask = append(updateMask, "locations")
}

if d.HasChange("effective_labels") {
updateMask = append(updateMask, "labels")
}
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
// won't set it
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -463,7 +483,18 @@ func flattenActiveDirectoryDomainName(v interface{}, d *schema.ResourceData, con
}

func flattenActiveDirectoryDomainLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
if v == nil {
return v
}

transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("labels"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}

return transformed
}

func flattenActiveDirectoryDomainAuthorizedNetworks(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand All @@ -489,15 +520,23 @@ func flattenActiveDirectoryDomainFqdn(v interface{}, d *schema.ResourceData, con
return v
}

func expandActiveDirectoryDomainLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
func flattenActiveDirectoryDomainTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return map[string]string{}, nil
return v
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)

transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("terraform_labels"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}
return m, nil

return transformed
}

func flattenActiveDirectoryDomainEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func expandActiveDirectoryDomainAuthorizedNetworks(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
Expand All @@ -516,3 +555,14 @@ func expandActiveDirectoryDomainLocations(v interface{}, d tpgresource.Terraform
func expandActiveDirectoryDomainAdmin(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandActiveDirectoryDomainEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestAccActiveDirectoryDomain_update(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"domain_name"},
ImportStateVerifyIgnore: []string{"domain_name", "labels", "terraform_labels"},
},
{
Config: testAccADDomainUpdate(context),
Expand All @@ -50,7 +50,7 @@ func TestAccActiveDirectoryDomain_update(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"domain_name"},
ImportStateVerifyIgnore: []string{"domain_name", "labels", "terraform_labels"},
},
{
Config: testAccADDomainBasic(context),
Expand All @@ -59,7 +59,7 @@ func TestAccActiveDirectoryDomain_update(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"domain_name"},
ImportStateVerifyIgnore: []string{"domain_name", "labels", "terraform_labels"},
},
},
})
Expand Down
Loading

0 comments on commit 23a2c59

Please sign in to comment.