Skip to content

Commit fd062f7

Browse files
Merge pull request hashicorp#517 from julianvmodesto/gcp_auth_backend_role-import
Support GCP auth backend role import
2 parents 00cbe32 + 18a63f0 commit fd062f7

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

vault/resource_gcp_auth_backend_role.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func gcpAuthBackendRoleResource() *schema.Resource {
149149
Update: gcpAuthResourceUpdate,
150150
Read: gcpAuthResourceRead,
151151
Delete: gcpAuthResourceDelete,
152+
Exists: gcpAuthResourceExists,
153+
Importer: &schema.ResourceImporter{
154+
State: schema.ImportStatePassthrough,
155+
},
152156
Schema: fields,
153157
}
154158
}
@@ -278,6 +282,17 @@ func gcpAuthResourceRead(d *schema.ResourceData, meta interface{}) error {
278282
return nil
279283
}
280284

285+
backend, err := gcpAuthResourceBackendFromPath(path)
286+
if err != nil {
287+
return fmt.Errorf("invalid path %q for GCP auth backend role: %s", path, err)
288+
}
289+
d.Set("backend", backend)
290+
role, err := gcpAuthResourceRoleFromPath(path)
291+
if err != nil {
292+
return fmt.Errorf("invalid path %q for GCP auth backend role: %s", path, err)
293+
}
294+
d.Set("role", role)
295+
281296
readTokenFields(d, resp)
282297

283298
// Check if the user is using the deprecated `policies`
@@ -367,3 +382,33 @@ func gcpAuthResourceDelete(d *schema.ResourceData, meta interface{}) error {
367382

368383
return nil
369384
}
385+
386+
func gcpAuthResourceExists(d *schema.ResourceData, meta interface{}) (bool, error) {
387+
client := meta.(*api.Client)
388+
path := d.Id()
389+
390+
log.Printf("[DEBUG] Checking if gcp auth role %q exists", path)
391+
resp, err := client.Logical().Read(path)
392+
if err != nil {
393+
return true, fmt.Errorf("error checking for existence of gcp auth resource config %q: %s", path, err)
394+
}
395+
log.Printf("[DEBUG] Checked if gcp auth role %q exists", path)
396+
397+
return resp != nil, nil
398+
}
399+
400+
func gcpAuthResourceBackendFromPath(path string) (string, error) {
401+
var parts = strings.Split(path, "/")
402+
if len(parts) != 4 {
403+
return "", fmt.Errorf("Expecdted 4 parts in path '%s'", path)
404+
}
405+
return parts[1], nil
406+
}
407+
408+
func gcpAuthResourceRoleFromPath(path string) (string, error) {
409+
var parts = strings.Split(path, "/")
410+
if len(parts) != 4 {
411+
return "", fmt.Errorf("Expecdted 4 parts in path '%s'", path)
412+
}
413+
return parts[3], nil
414+
}

vault/resource_gcp_auth_backend_role_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func TestGCPAuthBackendRole_basic(t *testing.T) {
4848
"token_policies.#", "0"),
4949
),
5050
},
51+
{
52+
ResourceName: "vault_gcp_auth_backend_role.test",
53+
ImportState: true,
54+
ImportStateVerify: true,
55+
},
5156
},
5257
})
5358
}

website/docs/r/gcp_auth_backend.html.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ In addition to the fields above, the following attributes are also exposed:
3737
* `project_id` - The GCP Project ID
3838

3939
* `client_email` - The clients email associated with the credentials
40+
41+
## Import
42+
43+
GCP authentication backends can be imported using the backend name, e.g.
44+
45+
```
46+
$ terraform import vault_gcp_auth_backend.gcp gcp
47+
```

website/docs/r/gcp_auth_backend_role.html.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,12 @@ documented above.
123123
## Attribute Reference
124124

125125
No additional attributes are exposed by this resource.
126+
127+
128+
## Import
129+
130+
GCP authentication roles can be imported using the `path`, e.g.
131+
132+
```
133+
$ terraform import vault_gcp_auth_backend_role.my_role auth/gcp/role/my_role
134+
```

0 commit comments

Comments
 (0)