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

Alias support for Key Policies #3768

Merged
merged 1 commit into from
May 25, 2022
Merged

Conversation

harshit777
Copy link
Contributor

Added alias support for KeyPolicies management

@harshit777 harshit777 changed the title Alias support for policies Alias support for Key Policies May 11, 2022
@harshit777 harshit777 force-pushed the AliasExtension branch 4 times, most recently from 8dab582 to c245bbc Compare May 17, 2022 11:55
Copy link

@dinesh-venkatraman-g dinesh-venkatraman-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attach the test results.

@@ -160,6 +167,7 @@ func dataSourceIBMKMSKeyPoliciesRead(context context.Context, d *schema.Resource
instanceID = CrnInstanceID[len(CrnInstanceID)-3]
}
endpointType := d.Get("endpoint_type").(string)
alias := d.Get("alias").(string)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. when we use ExactlyOneOf , can we still provide both the params in the input? (ie) both key_id and alias in our case
  2. If yes, if I provide a incorrect combination, say exiting key_id but an incorrect alias name in the Datasource, what is the current behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, both the params are not allowed (Exactly one of) require either of the input from the user and will output error if both are used. for ex.

  • Invalid combination of arguments: "key_id": only one of alias,key_id can be specified, but alias,key_id were specified.
  • Invalid combination of arguments: "alias": only one of alias,key_id can be specified, but alias,key_id were specified.
    2022/05/19 11:20:40 [ERROR] : eval: *terraform.EvalSequence, err: 2 problems:

This comes as an output error when used in combination of both the inputs

resource "ibm_kms_key_policies" "root" {
instance_id = ibm_resource_instance.kms_instance.id
key_id = ibm_kms_key.root.key_id
alias = ibm_kms_key_alias.alias.alias
rotation {
interval_month = 3
}
dual_auth_delete {
enabled = false
}
}

} else {
id = alias
}
key, err := api.GetKey(context, id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetKey need only when the key_id is empty

resource.TestCheckResourceAttr("ibm_kms_key.test", "key_name", keyName),
resource.TestCheckResourceAttr("ibm_kms_key_alias.testAlias", "alias", aliasName),
resource.TestCheckResourceAttr("ibm_kms_key_alias.testAlias2", "existing_alias", aliasName),
resource.TestCheckResourceAttr("ibm_kms_key_alias.testAlias2", "alias", aliasName2),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the attribute name ibm_kms_key_alias.testAlias2 is repeating

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two attributes are testing two different variables existing_alias and alias

@@ -26,13 +31,15 @@ The following arguments are supported:

- `endpoint_type` - (Optional, String) The type of the public or private endpoint to be used for fetching keys.
- `instance_id` - (Required, string) The keyprotect instance guid.
- `key_id` - (Required, string) The id of the key.
- `key_id` - (Required - If the alias is not provided, String) The id of the key.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `key_id` - (Required - If the alias is not provided, String) The id of the key.
- `key_id` - (Required - if the alias is not provided, String) The id of the key.

@@ -26,13 +31,15 @@ The following arguments are supported:

- `endpoint_type` - (Optional, String) The type of the public or private endpoint to be used for fetching keys.
- `instance_id` - (Required, string) The keyprotect instance guid.
- `key_id` - (Required, string) The id of the key.
- `key_id` - (Required - If the alias is not provided, String) The id of the key.
- `alias` - (Required - If the key_id is not provided, String) The alias of the key.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `alias` - (Required - If the key_id is not provided, String) The alias of the key.
- `alias` - (Required - if the key_id is not provided, String) The alias of the key.


- `alias` - (Required, Forces new resource, String) The alias name of the key.
- `endpoint_type` - (Optional, Forces new resource, String) The type of the public endpoint, or private endpoint to be used for creating keys.
- `instance_id` - (Required, Forces new resource, String) The hs-crypto or key protect instance GUID.
- `key_id` - (Required, String) The key ID for which alias has to be created.
- `existing_alias` - (Required, If the key_id is not provided, String) Existing Alias of the key.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `existing_alias` - (Required, If the key_id is not provided, String) Existing Alias of the key.
- `existing_alias` - (Required - if the key_id is not provided, String) Existing Alias of the key.


- `alias` - (Required, Forces new resource, String) The alias name of the key.
- `endpoint_type` - (Optional, Forces new resource, String) The type of the public endpoint, or private endpoint to be used for creating keys.
- `instance_id` - (Required, Forces new resource, String) The hs-crypto or key protect instance GUID.
- `key_id` - (Required, String) The key ID for which alias has to be created.
- `existing_alias` - (Required, If the key_id is not provided, String) Existing Alias of the key.
- `key_id` - (Required, If the alias is not provided, String) The key ID for which alias has to be created.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `key_id` - (Required, If the alias is not provided, String) The key ID for which alias has to be created.
- `key_id` - (Required - if the alias is not provided, String) The key ID for which alias has to be created.

```

## Argument reference

The following arguments are supported:

- `endpoint_type` - (Optional, String) The type of the public or private endpoint to be used for fetching policies.
- `key_id` - (Required - If the alias is not provided, String) The ID of the key.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `key_id` - (Required - If the alias is not provided, String) The ID of the key.
- `key_id` - (Required - if the alias is not provided, String) The ID of the key.

```

## Argument reference

The following arguments are supported:

- `endpoint_type` - (Optional, String) The type of the public or private endpoint to be used for fetching policies.
- `key_id` - (Required - If the alias is not provided, String) The ID of the key.
- `alias` - (Required - If the key_id is not provided, String) The alias created for the key.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `alias` - (Required - If the key_id is not provided, String) The alias created for the key.
- `alias` - (Required - if the key_id is not provided, String) The alias created for the key.

@harshit777
Copy link
Contributor Author

harshit777 commented May 20, 2022

Screenshot 2022-05-20 at 4 49 37 PM

Screenshot 2022-05-20 at 4 50 44 PM

Screenshot 2022-05-20 at 4 58 22 PM

Screenshot 2022-05-20 at 4 59 15 PM

Screenshot 2022-05-20 at 5 04 08 PM

@harshit777
Copy link
Contributor Author

Screenshot 2022-05-23 at 4 42 30 PM

Screenshot 2022-05-23 at 4 44 06 PM

@hkantare
Copy link
Collaborator

And share the testcases results of the reosurces and datasources

@@ -160,6 +167,7 @@ func dataSourceIBMKMSKeyPoliciesRead(context context.Context, d *schema.Resource
instanceID = CrnInstanceID[len(CrnInstanceID)-3]
}
endpointType := d.Get("endpoint_type").(string)
alias := d.Get("alias").(string)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use d.GetOK for both key_id and alias

var id string
	if v, ok := d.GetOk("key_id");ok {
		id =v.(string)
	} 
	if v, ok := d.GetOk("alias");ok {
		id = v.(string)
		key, err := api.GetKey(context, id)
		if err != nil {
			return diag.Errorf("Failed to get Key: %s", err)
		}
		d.Set("key_id", key.ID)
	}
	```

@@ -86,7 +94,14 @@ func resourceIBMKmsKeyAliasCreate(d *schema.ResourceData, meta interface{}) erro

aliasName := d.Get("alias").(string)
keyID := d.Get("key_id").(string)
stkey, err := kpAPI.CreateKeyAlias(context.Background(), aliasName, keyID)
aliasID := d.Get("existing_alias").(string)
var id string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the same concept of d.GetOk for optional fields in all files

@hkantare hkantare merged commit 8d78ee7 into IBM-Cloud:master May 25, 2022
SunithaGudisagarIBM pushed a commit to ibm-vpc/terraform-provider-ibm that referenced this pull request Sep 14, 2022
SunithaGudisagarIBM pushed a commit to ibm-vpc/terraform-provider-ibm that referenced this pull request Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants