Skip to content

fix(apigee): Error of update in google_apigee_developer #8728

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

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/12347.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
apigee: fixed error of update in `google_apigee_developer` resource
```
59 changes: 14 additions & 45 deletions google-beta/services/apigee/resource_apigee_developer.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,58 +311,27 @@ func resourceApigeeDeveloperUpdate(d *schema.ResourceData, meta interface{}) err

log.Printf("[DEBUG] Updating Developer %q: %#v", d.Id(), obj)
headers := make(http.Header)
updateMask := []string{}

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

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

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

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

if d.HasChange("attributes") {
updateMask = append(updateMask, "attributes")
}
// 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, ",")})
if err != nil {
return err
}

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}

// if updateMask is empty we are not updating anything so skip the post
if len(updateMask) > 0 {
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PUT",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
Headers: headers,
})

if err != nil {
return fmt.Errorf("Error updating Developer %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating Developer %q: %#v", d.Id(), res)
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "PUT",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutUpdate),
Headers: headers,
})

if err != nil {
return fmt.Errorf("Error updating Developer %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating Developer %q: %#v", d.Id(), res)
}

return resourceApigeeDeveloperRead(d, meta)
Expand Down
125 changes: 125 additions & 0 deletions google-beta/services/apigee/resource_apigee_developer_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package apigee_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
)

func TestAccApigeeDeveloper_apigeeDeveloperUpdateTest(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
"org_id": envvar.GetTestOrgFromEnv(t),
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckApigeeDeveloperDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccApigeeDeveloper_apigeeDeveloperBasicTestExample(context),
},
{
ResourceName: "google_apigee_developer.apigee_developer",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"org_id"},
},
{
Config: testAccApigeeDeveloper_apigeeDeveloperUpdateTest(context),
},
{
ResourceName: "google_apigee_developer.apigee_developer",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"org_id"},
},
},
})
}

func testAccApigeeDeveloper_apigeeDeveloperUpdateTest(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_project" "project" {
project_id = "tf-test%{random_suffix}"
name = "tf-test%{random_suffix}"
org_id = "%{org_id}"
billing_account = "%{billing_account}"
deletion_policy = "DELETE"
}

resource "google_project_service" "apigee" {
project = google_project.project.project_id
service = "apigee.googleapis.com"
}

resource "google_project_service" "compute" {
project = google_project.project.project_id
service = "compute.googleapis.com"
}

resource "google_project_service" "servicenetworking" {
project = google_project.project.project_id
service = "servicenetworking.googleapis.com"
}

resource "google_compute_network" "apigee_network" {
name = "apigee-network"
project = google_project.project.project_id
depends_on = [google_project_service.compute]
}

resource "google_compute_global_address" "apigee_range" {
name = "apigee-range"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.apigee_network.id
project = google_project.project.project_id
}

resource "google_service_networking_connection" "apigee_vpc_connection" {
network = google_compute_network.apigee_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.apigee_range.name]
depends_on = [google_project_service.servicenetworking]
}

resource "google_apigee_organization" "apigee_org" {
analytics_region = "us-central1"
project_id = google_project.project.project_id
authorized_network = google_compute_network.apigee_network.id
depends_on = [
google_service_networking_connection.apigee_vpc_connection,
google_project_service.apigee,
]
}

resource "google_apigee_instance" "apigee_instance" {
name = "tf-test%{random_suffix}"
location = "us-central1"
org_id = google_apigee_organization.apigee_org.id
peering_cidr_range = "SLASH_22"
}

resource "google_apigee_developer" "apigee_developer" {
email = "tf-test%{random_suffix}@acme.com"
first_name = "Yamada"
last_name = "Taro"
user_name = "yamada.taro"
org_id = google_apigee_organization.apigee_org.id
depends_on = [
google_apigee_instance.apigee_instance
]
}
`, context)
}