diff --git a/ibm/service/vpc/data_source_ibm_is_ssh_key.go b/ibm/service/vpc/data_source_ibm_is_ssh_key.go index d702296042..acba580905 100644 --- a/ibm/service/vpc/data_source_ibm_is_ssh_key.go +++ b/ibm/service/vpc/data_source_ibm_is_ssh_key.go @@ -23,6 +23,14 @@ func DataSourceIBMISSSHKey() *schema.Resource { Description: "Resource group ID", }, + "tags": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: flex.ResourceIBMVPCHash, + Description: "User Tags for the ssh", + }, + isKeyName: { Type: schema.TypeString, Required: true, @@ -150,6 +158,12 @@ func keyGetByName(d *schema.ResourceData, meta interface{}, name string) error { if key.PublicKey != nil { d.Set(isKeyPublicKey, *key.PublicKey) } + tags, err := flex.GetGlobalTagsUsingCRN(meta, *key.CRN, "", isUserTagType) + if err != nil { + log.Printf( + "Error on get of resource vpc ssh key (%s) tags: %s", d.Id(), err) + } + d.Set("tags", tags) accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *key.CRN, "", isKeyAccessTagType) if err != nil { log.Printf( diff --git a/ibm/service/vpc/data_source_ibm_is_ssh_key_test.go b/ibm/service/vpc/data_source_ibm_is_ssh_key_test.go index d4bbc2a13d..a45e71325f 100644 --- a/ibm/service/vpc/data_source_ibm_is_ssh_key_test.go +++ b/ibm/service/vpc/data_source_ibm_is_ssh_key_test.go @@ -29,6 +29,22 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( "data.ibm_is_ssh_key.ds_key", "name", name1), + resource.TestCheckResourceAttrSet( + "data.ibm_is_ssh_key.ds_key", "crn"), + resource.TestCheckResourceAttrSet( + "data.ibm_is_ssh_key.ds_key", "fingerprint"), + resource.TestCheckResourceAttrSet( + "data.ibm_is_ssh_key.ds_key", "id"), + resource.TestCheckResourceAttrSet( + "data.ibm_is_ssh_key.ds_key", "length"), + resource.TestCheckResourceAttrSet( + "data.ibm_is_ssh_key.ds_key", "public_key"), + resource.TestCheckResourceAttrSet( + "data.ibm_is_ssh_key.ds_key", "type"), + resource.TestCheckResourceAttrSet( + "data.ibm_is_ssh_key.ds_key", "tags.#"), + resource.TestCheckResourceAttr( + "data.ibm_is_ssh_key.ds_key", "tags.#", "3"), ), }, }, @@ -38,8 +54,9 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE func testDSCheckIBMISSSHKeyConfig(publicKey, name string) string { return fmt.Sprintf(` resource "ibm_is_ssh_key" "key" { - name = "%s" - public_key = "%s" + name = "%s" + public_key = "%s" + tags = ["test:1", "test:2", "test:3"] } data "ibm_is_ssh_key" "ds_key" { name = "${ibm_is_ssh_key.key.name}" diff --git a/ibm/service/vpc/data_source_ibm_is_ssh_keys.go b/ibm/service/vpc/data_source_ibm_is_ssh_keys.go index 8978b2b2af..7116438bfd 100644 --- a/ibm/service/vpc/data_source_ibm_is_ssh_keys.go +++ b/ibm/service/vpc/data_source_ibm_is_ssh_keys.go @@ -109,6 +109,13 @@ func DataSourceIBMIsSshKeys() *schema.Resource { Computed: true, Description: "The crypto-system used by this key.", }, + "tags": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: flex.ResourceIBMVPCHash, + Description: "User Tags for the ssh", + }, isKeyAccessTags: { Type: schema.TypeSet, Computed: true, @@ -154,7 +161,6 @@ func dataSourceIBMIsSshKeysRead(context context.Context, d *schema.ResourceData, } d.SetId(dataSourceIBMIsSshKeysID(d)) - err = d.Set(isKeys, dataSourceKeyCollectionFlattenKeys(allrecs, d, meta)) if err != nil { return diag.FromErr(fmt.Errorf("Error setting keys %s", err)) @@ -212,6 +218,12 @@ func dataSourceKeyCollectionKeysToMap(keysItem vpcv1.Key, d *schema.ResourceData if keysItem.Type != nil { keysMap[isKeyType] = keysItem.Type } + tags, err := flex.GetGlobalTagsUsingCRN(meta, *keysItem.CRN, "", isUserTagType) + if err != nil { + log.Printf( + "Error on get of resource vpc SSH Key (%s) user tags: %s", d.Id(), err) + } + keysMap["tags"] = tags accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *keysItem.CRN, "", isKeyAccessTagType) if err != nil { log.Printf( diff --git a/ibm/service/vpc/data_source_ibm_is_ssh_keys_test.go b/ibm/service/vpc/data_source_ibm_is_ssh_keys_test.go index b6c154cc67..ae037c363f 100644 --- a/ibm/service/vpc/data_source_ibm_is_ssh_keys_test.go +++ b/ibm/service/vpc/data_source_ibm_is_ssh_keys_test.go @@ -5,9 +5,11 @@ package vpc_test import ( "fmt" + "strings" "testing" acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) @@ -26,6 +28,41 @@ func TestAccIBMIsSshKeysDataSourceBasic(t *testing.T) { }, }) } +func TestAccIBMIsSshKeysDataSourceTags(t *testing.T) { + name1 := fmt.Sprintf("tfssh-name-%d", acctest.RandIntRange(10, 100)) + publicKey := strings.TrimSpace(` +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR +`) + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIBMIsSshKeysDataSourceConfigTags(publicKey, name1), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "ibm_is_ssh_key.key", "name", name1), + resource.TestCheckResourceAttrSet( + "ibm_is_ssh_key.key", "crn"), + resource.TestCheckResourceAttrSet( + "ibm_is_ssh_key.key", "fingerprint"), + resource.TestCheckResourceAttrSet( + "ibm_is_ssh_key.key", "id"), + resource.TestCheckResourceAttrSet( + "ibm_is_ssh_key.key", "length"), + resource.TestCheckResourceAttrSet("data.ibm_is_ssh_keys.is_ssh_keys", "id"), + resource.TestCheckResourceAttrSet("data.ibm_is_ssh_keys.is_ssh_keys", "keys.#"), + resource.TestCheckResourceAttrSet("data.ibm_is_ssh_keys.is_ssh_keys", "keys.0.name"), + resource.TestCheckResourceAttrSet("data.ibm_is_ssh_keys.is_ssh_keys", "keys.0.crn"), + resource.TestCheckResourceAttrSet("data.ibm_is_ssh_keys.is_ssh_keys", "keys.0.fingerprint"), + resource.TestCheckResourceAttrSet("data.ibm_is_ssh_keys.is_ssh_keys", "keys.0.id"), + resource.TestCheckResourceAttrSet("data.ibm_is_ssh_keys.is_ssh_keys", "keys.0.tags.#"), + resource.TestCheckResourceAttr("data.ibm_is_ssh_keys.is_ssh_keys", "keys.0.tags.#", "3"), + ), + }, + }, + }) +} func testAccCheckIBMIsSshKeysDataSourceConfigBasic() string { return fmt.Sprintf(` @@ -33,3 +70,15 @@ func testAccCheckIBMIsSshKeysDataSourceConfigBasic() string { } `) } +func testAccCheckIBMIsSshKeysDataSourceConfigTags(publicKey, name1 string) string { + return fmt.Sprintf(` + resource "ibm_is_ssh_key" "key" { + name = "%s" + public_key = "%s" + tags = ["test:1", "test:2", "test:3"] + } + data "ibm_is_ssh_keys" "is_ssh_keys" { + depends_on = [ ibm_is_ssh_key.key ] + } + `, name1, publicKey) +} diff --git a/website/docs/d/is_ssh_key.html.markdown b/website/docs/d/is_ssh_key.html.markdown index a74b822ee2..024a66ac08 100644 --- a/website/docs/d/is_ssh_key.html.markdown +++ b/website/docs/d/is_ssh_key.html.markdown @@ -44,4 +44,5 @@ In addition to all argument reference list, you can access the following attribu - `fingerprint`- (String) The SHA256 fingerprint of the public key. - `length` - (String) The length of the SSH key. - `public_key` - (String) The public SSH key value. +- `tags` - (List) User tags associated for the ssh key. - `type` - (String) The crypto system that is used by this key. diff --git a/website/docs/d/is_ssh_keys.html.markdown b/website/docs/d/is_ssh_keys.html.markdown index 03345f63b7..9a304e4a77 100644 --- a/website/docs/d/is_ssh_keys.html.markdown +++ b/website/docs/d/is_ssh_keys.html.markdown @@ -46,4 +46,5 @@ In addition to all argument references listed, you can access the following attr - Constraints: The value must match regular expression `/^[0-9a-f]{32}$/`. - `name` - (String) The user-defined name for this resource group. - Constraints: The maximum length is `40` characters. The minimum length is `1` character. The value must match regular expression `/^[a-zA-Z0-9-_ ]+$/`. - - `type` - (String) The crypto-system used by this key. \ No newline at end of file + - `type` - (String) The crypto-system used by this key. + - `tags` - (List) User tags associated for the ssh key.