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

adds tags support for is_ssh_key(s) datasource #5175

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 19 additions & 2 deletions ibm/service/vpc/data_source_ibm_is_ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand All @@ -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}"
Expand Down
14 changes: 13 additions & 1 deletion ibm/service/vpc/data_source_ibm_is_ssh_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand Down
49 changes: 49 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_ssh_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -26,10 +28,57 @@ 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(`
data "ibm_is_ssh_keys" "is_ssh_keys" {
}
`)
}
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)
}
1 change: 1 addition & 0 deletions website/docs/d/is_ssh_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion website/docs/d/is_ssh_keys.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- `type` - (String) The crypto-system used by this key.
- `tags` - (List) User tags associated for the ssh key.
Loading