-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Identity V3: Add application credential resource (#660)
- Loading branch information
Showing
15 changed files
with
837 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package openstack | ||
|
||
import ( | ||
"github.com/gophercloud/gophercloud/openstack/identity/v3/applicationcredentials" | ||
) | ||
|
||
func flattenIdentityApplicationCredentialRolesV3(roles []applicationcredentials.Role) []string { | ||
var res []string | ||
for _, role := range roles { | ||
res = append(res, role.Name) | ||
} | ||
return res | ||
} | ||
|
||
func expandIdentityApplicationCredentialRolesV3(roles []interface{}) []applicationcredentials.Role { | ||
var res []applicationcredentials.Role | ||
for _, role := range roles { | ||
res = append(res, applicationcredentials.Role{Name: role.(string)}) | ||
} | ||
return res | ||
} |
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,43 @@ | ||
package openstack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/gophercloud/gophercloud/openstack/identity/v3/applicationcredentials" | ||
) | ||
|
||
func TestFlattenIdentityApplicationCredentialRolesV3(t *testing.T) { | ||
role1 := applicationcredentials.Role{ | ||
ID: "123", | ||
Name: "foo", | ||
} | ||
role2 := applicationcredentials.Role{ | ||
ID: "321", | ||
Name: "bar", | ||
} | ||
|
||
roles := []applicationcredentials.Role{role1, role2} | ||
|
||
expected := []string{"foo", "bar"} | ||
|
||
actual := flattenIdentityApplicationCredentialRolesV3(roles) | ||
assert.Equal(t, expected, actual) | ||
} | ||
|
||
func TestExpandIdentityApplicationCredentialRolesV3(t *testing.T) { | ||
role1 := applicationcredentials.Role{ | ||
Name: "foo", | ||
} | ||
role2 := applicationcredentials.Role{ | ||
Name: "bar", | ||
} | ||
|
||
roles := []interface{}{role1.Name, role2.Name} | ||
|
||
expected := []applicationcredentials.Role{role1, role2} | ||
|
||
actual := expandIdentityApplicationCredentialRolesV3(roles) | ||
assert.Equal(t, expected, actual) | ||
} |
51 changes: 51 additions & 0 deletions
51
openstack/import_openstack_identity_application_credential_v3_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,51 @@ | ||
package openstack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccIdentityV3ApplicationCredential_importBasic(t *testing.T) { | ||
resourceName := "openstack_identity_application_credential_v3.app_cred_1" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckIdentityV3ApplicationCredentialDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccIdentityV3ApplicationCredential_basic, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"secret"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccIdentityV3ApplicationCredential_importCustomSecret(t *testing.T) { | ||
resourceName := "openstack_identity_application_credential_v3.app_cred_1" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckIdentityV3ApplicationCredentialDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccIdentityV3ApplicationCredential_custom_secret, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"secret"}, | ||
}, | ||
}, | ||
}) | ||
} |
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
191 changes: 191 additions & 0 deletions
191
openstack/resource_openstack_identity_application_credential_v3.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,191 @@ | ||
package openstack | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
"github.com/gophercloud/gophercloud/openstack/identity/v3/applicationcredentials" | ||
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func resourceIdentityApplicationCredentialV3() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceIdentityApplicationCredentialV3Create, | ||
Read: resourceIdentityApplicationCredentialV3Read, | ||
Delete: resourceIdentityApplicationCredentialV3Delete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"unrestricted": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
ForceNew: true, | ||
}, | ||
|
||
"secret": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Sensitive: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"project_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"roles": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
|
||
"expires_at": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.ValidateRFC3339TimeString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceIdentityApplicationCredentialV3Create(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
identityClient, err := config.identityV3Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack identity client: %s", err) | ||
} | ||
|
||
token := tokens.Get(identityClient, config.OsClient.TokenID) | ||
if token.Err != nil { | ||
return token.Err | ||
} | ||
|
||
user, err := token.ExtractUser() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
createOpts := applicationcredentials.CreateOpts{ | ||
Name: d.Get("name").(string), | ||
Description: d.Get("description").(string), | ||
Unrestricted: d.Get("unrestricted").(bool), | ||
Roles: expandIdentityApplicationCredentialRolesV3(d.Get("roles").(*schema.Set).List()), | ||
ExpiresAt: d.Get("expires_at").(string), | ||
} | ||
|
||
log.Printf("[DEBUG] openstack_identity_application_credential_v3 create options: %#v", createOpts) | ||
|
||
createOpts.Secret = d.Get("secret").(string) | ||
|
||
applicationCredential, err := applicationcredentials.Create(identityClient, user.ID, createOpts).Extract() | ||
if err != nil { | ||
if v, ok := err.(gophercloud.ErrDefault404); ok { | ||
return fmt.Errorf("Error creating openstack_identity_application_credential_v3: %s", v.ErrUnexpectedResponseCode.Body) | ||
} | ||
return fmt.Errorf("Error creating openstack_identity_application_credential_v3: %s", err) | ||
} | ||
|
||
d.SetId(applicationCredential.ID) | ||
|
||
// Secret is returned only once | ||
d.Set("secret", applicationCredential.Secret) | ||
|
||
return resourceIdentityApplicationCredentialV3Read(d, meta) | ||
} | ||
|
||
func resourceIdentityApplicationCredentialV3Read(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
identityClient, err := config.identityV3Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack identity client: %s", err) | ||
} | ||
|
||
token := tokens.Get(identityClient, config.OsClient.TokenID) | ||
if token.Err != nil { | ||
return token.Err | ||
} | ||
|
||
user, err := token.ExtractUser() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
applicationCredential, err := applicationcredentials.Get(identityClient, user.ID, d.Id()).Extract() | ||
if err != nil { | ||
return CheckDeleted(d, err, "Error retrieving openstack_identity_application_credential_v3") | ||
} | ||
|
||
log.Printf("[DEBUG] Retrieved openstack_identity_application_credential_v3 %s: %#v", d.Id(), applicationCredential) | ||
|
||
d.Set("name", applicationCredential.Name) | ||
d.Set("description", applicationCredential.Description) | ||
d.Set("unrestricted", applicationCredential.Unrestricted) | ||
d.Set("roles", flattenIdentityApplicationCredentialRolesV3(applicationCredential.Roles)) | ||
d.Set("project_id", applicationCredential.ProjectID) | ||
d.Set("region", GetRegion(d, config)) | ||
|
||
if applicationCredential.ExpiresAt == (time.Time{}) { | ||
d.Set("expires_at", "") | ||
} else { | ||
d.Set("expires_at", applicationCredential.ExpiresAt.UTC().Format(time.RFC3339)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceIdentityApplicationCredentialV3Delete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
identityClient, err := config.identityV3Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating OpenStack identity client: %s", err) | ||
} | ||
|
||
token := tokens.Get(identityClient, config.OsClient.TokenID) | ||
if token.Err != nil { | ||
return token.Err | ||
} | ||
|
||
user, err := token.ExtractUser() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = applicationcredentials.Delete(identityClient, user.ID, d.Id()).ExtractErr() | ||
if err != nil { | ||
return CheckDeleted(d, err, "Error deleting openstack_identity_application_credential_v3") | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.