Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply new labels model to more resources (part 1) #6294

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-beta/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
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func ResourceActiveDirectoryPeering() *schema.Resource {
},

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

Expand Down Expand Up @@ -82,11 +83,24 @@ func ResourceActiveDirectoryPeering() *schema.Resource {
Optional: true,
Description: `Additional information about the current status of this peering, if available.`,
},
"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},
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `Unique name of the peering in this scope including projects and location using the form: projects/{projectId}/locations/global/peerings/{peeringId}.`,
},
"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 @@ -106,12 +120,6 @@ func resourceActiveDirectoryPeeringCreate(d *schema.ResourceData, meta interface
}

obj := make(map[string]interface{})
labelsProp, err := expandActiveDirectoryPeeringLabels(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
}
authorizedNetworkProp, err := expandActiveDirectoryPeeringAuthorizedNetwork(d.Get("authorized_network"), d, config)
if err != nil {
return err
Expand All @@ -130,6 +138,12 @@ func resourceActiveDirectoryPeeringCreate(d *schema.ResourceData, meta interface
} else if v, ok := d.GetOkExists("status_message"); !tpgresource.IsEmptyValue(reflect.ValueOf(statusMessageProp)) && (ok || !reflect.DeepEqual(v, statusMessageProp)) {
obj["statusMessage"] = statusMessageProp
}
labelsProp, err := expandActiveDirectoryPeeringEffectiveLabels(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/peerings?peeringId={{peering_id}}")
if err != nil {
Expand Down Expand Up @@ -251,6 +265,12 @@ func resourceActiveDirectoryPeeringRead(d *schema.ResourceData, meta interface{}
if err := d.Set("domain_resource", flattenActiveDirectoryPeeringDomainResource(res["domainResource"], d, config)); err != nil {
return fmt.Errorf("Error reading Peering: %s", err)
}
if err := d.Set("terraform_labels", flattenActiveDirectoryPeeringTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Peering: %s", err)
}
if err := d.Set("effective_labels", flattenActiveDirectoryPeeringEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Peering: %s", err)
}

return nil
}
Expand All @@ -271,18 +291,18 @@ func resourceActiveDirectoryPeeringUpdate(d *schema.ResourceData, meta interface
billingProject = project

obj := make(map[string]interface{})
labelsProp, err := expandActiveDirectoryPeeringLabels(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
}
statusMessageProp, err := expandActiveDirectoryPeeringStatusMessage(d.Get("status_message"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("status_message"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, statusMessageProp)) {
obj["statusMessage"] = statusMessageProp
}
labelsProp, err := expandActiveDirectoryPeeringEffectiveLabels(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 Down Expand Up @@ -381,7 +401,18 @@ func flattenActiveDirectoryPeeringName(v interface{}, d *schema.ResourceData, co
}

func flattenActiveDirectoryPeeringLabels(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 flattenActiveDirectoryPeeringAuthorizedNetwork(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand All @@ -392,15 +423,23 @@ func flattenActiveDirectoryPeeringDomainResource(v interface{}, d *schema.Resour
return v
}

func expandActiveDirectoryPeeringLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
func flattenActiveDirectoryPeeringTerraformLabels(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 flattenActiveDirectoryPeeringEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func expandActiveDirectoryPeeringAuthorizedNetwork(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
Expand All @@ -414,3 +453,14 @@ func expandActiveDirectoryPeeringDomainResource(v interface{}, d tpgresource.Ter
func expandActiveDirectoryPeeringStatusMessage(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandActiveDirectoryPeeringEffectiveLabels(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
}
Loading