Skip to content

Commit

Permalink
Fix IBM-Cloud#1387 IBM-Cloud#1232: made role as optional argument
Browse files Browse the repository at this point in the history
  • Loading branch information
kavya498 committed Dec 15, 2021
1 parent 3fea8f1 commit dae1d82
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions ibm/resource_ibm_resource_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func resourceIBMResourceKey() *schema.Resource {

"role": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
Description: "Name of the user role.Valid roles are Writer, Reader, Manager, Administrator, Operator, Viewer, Editor and Custom Roles.",
// ValidateFunc: validateRole,
Expand Down Expand Up @@ -190,7 +190,6 @@ func resourceIBMResourceKeyCreate(d *schema.ResourceData, meta interface{}) erro
return err
}
name := d.Get("name").(string)
role := d.Get("role").(string)

var instanceID, aliasID string
if insID, ok := d.GetOk("resource_instance_id"); ok {
Expand Down Expand Up @@ -235,19 +234,22 @@ func resourceIBMResourceKeyCreate(d *schema.ResourceData, meta interface{}) erro
if err != nil {
return fmt.Errorf("Error creating resource key when get service: %s", err)
}
serviceRole, err := getRoleFromName(role, service.Name, meta)
if err != nil {
return fmt.Errorf("Error creating resource key when get role: %s", err)
}

keyParameters.SetProperty("role_crn", serviceRole.RoleID)

resourceKeyCreate := rc.CreateResourceKeyOptions{
Name: &name,
Source: sourceCRN,
Role: serviceRole.RoleID,
Parameters: &keyParameters,
}
if r, ok := d.GetOk("role"); ok {
role := r.(string)
serviceRole, err := getRoleFromName(role, service.Name, meta)
if err != nil {
return fmt.Errorf("Error creating resource key when get role: %s", err)
}
keyParameters.SetProperty("role_crn", serviceRole.RoleID)
resourceKeyCreate.Role = serviceRole.RoleID
}

resourceKey, resp, err := rsContClient.CreateResourceKey(&resourceKeyCreate)
if err != nil {
return fmt.Errorf("Error creating resource key: %s with resp code: %s", err, resp)
Expand Down
4 changes: 2 additions & 2 deletions ibm/resource_ibm_resource_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestAccIBMResourceKey_Parameters(t *testing.T) {
})
}

func TestAccIBMResourceKeyWithCustomRole(t *testing.T) {
func TestAccIBMResourceKey_WithCustomRole(t *testing.T) {
resourceName := fmt.Sprintf("tf-cos-%d", acctest.RandIntRange(10, 100))
resourceKey := fmt.Sprintf("tf-cos-%d", acctest.RandIntRange(10, 100))
crName := fmt.Sprintf("Name%d", acctest.RandIntRange(10, 100))
Expand All @@ -111,7 +111,7 @@ func TestAccIBMResourceKeyWithCustomRole(t *testing.T) {
testAccCheckIBMResourceKeyExists("ibm_resource_key.resourceKey"),
resource.TestCheckResourceAttr("ibm_resource_key.resourceKey", "name", resourceKey),
resource.TestCheckResourceAttr("ibm_resource_key.resourceKey", "credentials.%", "7"),
resource.TestCheckResourceAttr("ibm_resource_key.resourceKey", "role", crName),
resource.TestCheckResourceAttr("ibm_resource_key.resourceKey", "role", displayName),
),
},
},
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/resource_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Review the argument references that you can specify for your resource.

- `name` - (Required, Forces new resource, String) A descriptive name used to identify a resource key.
- `parameters` (Optional, Map) Arbitrary parameters to pass to the resource in JSON format. If you want to create service credentials by using the private service endpoint, include the `service-endpoints = "private"` parameter.
- `role` - (Required, Forces new resource, String) The name of the user role. Valid roles are `Writer`, `Reader`, `Manager`, `Administrator`, `Operator`, `Viewer`, and `Editor`.
- `role` - (Optional, Forces new resource, String) The name of the user role. Valid roles are `Writer`, `Reader`, `Manager`, `Administrator`, `Operator`, `Viewer`, and `Editor`. This argument is Optional only during creation of service credentials for Cloud Databases and other non-IAM-enabled services and is Required for all other IAM-enabled services.
- `resource_instance_id` - (Optional, Forces new resource, String) The ID of the resource instance associated with the resource key. **Note** Conflicts with `resource_alias_id`.
- `resource_alias_id` - (Optional, Forces new resource, String) The ID of the resource alias associated with the resource key. **Note** Conflicts with `resource_instance_id`.
- `tags` (Optional, Array of strings) Tags associated with the resource key instance. **Note** Tags are managed locally and not stored on the IBM Cloud Service Endpoint at this moment.
Expand Down

0 comments on commit dae1d82

Please sign in to comment.