forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds storage_hmac_key_resource (GoogleCloudPlatform#3084)
* Adds hmac key * Fixes up hmacKey resource properties in terraform * Update products/storage/terraform.yaml Co-Authored-By: Riley Karson <[email protected]> * Update products/storage/api.yaml Co-Authored-By: Riley Karson <[email protected]> * Adds an example, website links, and several fixes from review * Adds test and fixes hmac_key id * tweaking hmac_key example * Update third_party/terraform/tests/resource_storage_hmac_key_test.go Co-Authored-By: Riley Karson <[email protected]> * Update products/storage/api.yaml Co-Authored-By: Riley Karson <[email protected]> * Update third_party/terraform/tests/resource_storage_hmac_key_test.go Co-Authored-By: Riley Karson <[email protected]> * hmac_key: adds doc warning about secrets, fixes yaml formatting, and improves test * More fixes for review * Fixing gofmt * fix resource name * more iteration * More fixes * RM unused var in hmac_key_test * hmac_key: Pre-delete and more fixes * Fixing wrong assumptions * Edits to support read schema * Get tests passing, handle deleted items properly * Sidebar - to _ * Remove the update encoder because the docs are wrong on update format anyways Co-authored-by: Riley Karson <[email protected]>
- Loading branch information
1 parent
5cfa9e2
commit 6471f4c
Showing
11 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
templates/terraform/custom_check_destroy/storage_hmac_key.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
url, err := replaceVarsForTest(config, rs, "{{StorageBasePath}}projects/{{project}}/hmacKeys/{{access_id}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := sendRequest(config, "GET", "", url, nil) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
if v := res["state"]; v == "DELETED" { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("StorageHmacKey still exists at %s", url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
if v := res["state"]; v == "DELETED" { | ||
return nil, nil | ||
} | ||
|
||
return res, nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
resource "google_service_account" "service_account" { | ||
account_id = "<%= ctx[:vars]['account_id'] %>" | ||
} | ||
|
||
resource "google_storage_hmac_key" "<%= ctx[:primary_resource_id] %>" { | ||
service_account_email = google_service_account.service_account.email | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// `secret` and `access_id` are generated by the API upon successful CREATE. The following | ||
// ensures terraform has the correct values based on the Projects.hmacKeys response object. | ||
secret, ok := res["secret"].(string) | ||
if !ok { | ||
return fmt.Errorf("The response to CREATE was missing an expected field. Your create did not work.") | ||
} | ||
|
||
d.Set("secret", secret) | ||
|
||
metadata := res["metadata"].(map[string]interface{}) | ||
accessId, ok := metadata["accessId"].(string) | ||
if !ok { | ||
return fmt.Errorf("The response to CREATE was missing an expected field. Your create did not work.") | ||
} | ||
|
||
d.Set("access_id", accessId) | ||
|
||
id, err = replaceVars(d, config, "projects/{{project}}/hmacKeys/{{access_id}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
|
||
d.SetId(id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
getUrl, err := replaceVars(d, config, "{{StorageBasePath}}projects/{{project}}/hmacKeys/{{access_id}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
getRes, err := sendRequest(config, "GET", project, getUrl, nil) | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("StorageHmacKey %q", d.Id())) | ||
} | ||
|
||
// HmacKeys need to be INACTIVE to be deleted and the API doesn't accept noop | ||
// updates | ||
if v := getRes["state"]; v == "ACTIVE" { | ||
getRes["state"] = "INACTIVE" | ||
updateUrl, err := replaceVars(d, config, "{{StorageBasePath}}projects/{{project}}/hmacKeys/{{access_id}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Deactivating HmacKey %q: %#v", d.Id(), getRes) | ||
_, err = sendRequestWithTimeout(config, "PUT", project, updateUrl, getRes, d.Timeout(schema.TimeoutUpdate)) | ||
if err != nil { | ||
return fmt.Errorf("Error deactivating HmacKey %q: %s", d.Id(), err) | ||
} | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
third_party/terraform/tests/resource_storage_hmac_key_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccStorageHmacKey_update(t *testing.T) { | ||
t.Parallel() | ||
|
||
saName := fmt.Sprintf("%v%v", "service-account", acctest.RandString(10)) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckStorageHmacKeyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccGoogleStorageHmacKeyBasic(saName, "ACTIVE"), | ||
}, | ||
{ | ||
ResourceName: "google_storage_hmac_key.key", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"secret"}, | ||
}, | ||
{ | ||
Config: testAccGoogleStorageHmacKeyBasic(saName, "INACTIVE"), | ||
}, | ||
{ | ||
ResourceName: "google_storage_hmac_key.key", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"secret"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccGoogleStorageHmacKeyBasic(saName, state string) string { | ||
return fmt.Sprintf(` | ||
resource "google_service_account" "service_account" { | ||
account_id = "%s" | ||
} | ||
resource "google_storage_hmac_key" "key" { | ||
service_account_email = google_service_account.service_account.email | ||
state = "%s" | ||
} | ||
`, saName, state) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters