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

allow setting a project on oslogin ssh key #2583

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/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-beta/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-beta/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%2Fgit.luolix.top%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