Skip to content

Commit

Permalink
allow setting a project on oslogin ssh key (#4085) (#7505)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 12, 2020
1 parent f39126e commit b317efd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/4085.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
oslogin: added ability to set a `project` on `google_os_login_ssh_public_key`
```
14 changes: 14 additions & 0 deletions google/resource_os_login_ssh_public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func resourceOSLoginSSHPublicKey() *schema.Resource {
Optional: true,
Description: `An expiration time in microseconds since epoch.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The project ID of the Google Cloud Platform project.`,
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -102,6 +108,14 @@ func resourceOSLoginSSHPublicKeyCreate(d *schema.ResourceData, meta interface{})
billingProject = bp
}

// Don't use `getProject()` because we only want to set the project in the URL
// if the user set it explicitly on the resource.
if p, ok := d.GetOk("project"); ok {
url, err = addQueryParams(url, map[string]string{"projectId": p.(string)})
if err != nil {
return err
}
}
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating SSHPublicKey: %s", err)
Expand Down
44 changes: 43 additions & 1 deletion google/resource_os_login_ssh_public_key_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAccOSLoginSSHPublicKey_osLoginSshKeyProvidedUserExample(t *testing.T) {
ResourceName: "google_os_login_ssh_public_key.cache",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"user"},
ImportStateVerifyIgnore: []string{"user", "project"},
},
},
})
Expand All @@ -63,6 +63,48 @@ resource "google_os_login_ssh_public_key" "cache" {
`, context)
}

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

context := map[string]interface{}{
"project": getTestProjectFromEnv(),
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
CheckDestroy: testAccCheckOSLoginSSHPublicKeyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccOSLoginSSHPublicKey_osLoginSshKeyWithProjectExample(context),
},
{
ResourceName: "google_os_login_ssh_public_key.cache",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"user", "project"},
},
},
})
}

func testAccOSLoginSSHPublicKey_osLoginSshKeyWithProjectExample(context map[string]interface{}) string {
return Nprintf(`
data "google_client_openid_userinfo" "me" {
}
resource "google_os_login_ssh_public_key" "cache" {
user = data.google_client_openid_userinfo.me.email
key = file("test-fixtures/ssh_rsa.pub")
project = "%{project}"
}
`, context)
}

func testAccCheckOSLoginSSHPublicKeyDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
22 changes: 22 additions & 0 deletions website/docs/r/os_login_ssh_public_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ resource "google_os_login_ssh_public_key" "cache" {
key = file("path/to/id_rsa.pub")
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=os_login_ssh_key_with_project&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Os Login Ssh Key With Project


```hcl
data "google_client_openid_userinfo" "me" {
}
resource "google_os_login_ssh_public_key" "cache" {
user = data.google_client_openid_userinfo.me.email
key = file("path/to/id_rsa.pub")
project = "my-project-name"
}
```

## Argument Reference

Expand All @@ -70,6 +88,10 @@ The following arguments are supported:
(Optional)
An expiration time in microseconds since epoch.

* `project` -
(Optional)
The project ID of the Google Cloud Platform project.


## Attributes Reference

Expand Down

0 comments on commit b317efd

Please sign in to comment.