-
Notifications
You must be signed in to change notification settings - Fork 359
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
Identity V3: Add application credential resource #660
Merged
jtopjian
merged 1 commit into
terraform-provider-openstack:master
from
kayrus:create-app-cred
Feb 20, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you think, this can break anything, let me know - I'll remove this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can constitute as a legitimate bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it ok to leave it here, or should I create a dedicated PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should really be a dedicated PR since it's has nothing to do with this PR and will have a changelog entry. We can leave this here, but something to keep in mind for future changes.