Skip to content

Commit

Permalink
New data source: bcc_key_pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBeng committed Aug 10, 2023
1 parent b288a0b commit 9d922c8
Show file tree
Hide file tree
Showing 20 changed files with 527 additions and 21 deletions.
2 changes: 2 additions & 0 deletions baiducloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ package baiducloud
import (
"bytes"
"fmt"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/bcc"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/bec"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/cdn"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/service/snic"
Expand Down Expand Up @@ -231,6 +232,7 @@ func Provider() terraform.ResourceProvider {
"baiducloud_snic_public_services": snic.DataSourcePublicServices(),
"baiducloud_bec_nodes": bec.DataSourceNodes(),
"baiducloud_bec_vm_instances": bec.DataSourceVMInstances(),
"baiducloud_bcc_key_pairs": bcc.DataSourceKeyPairs(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
21 changes: 21 additions & 0 deletions baiducloud/service/bcc/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package bcc

import "github.com/baidubce/bce-sdk-go/services/bcc/api"

func flattenKeyPairList(keyPairs []api.KeypairModel) interface{} {
tfList := []map[string]interface{}{}
for _, v := range keyPairs {
tfMap := map[string]interface{}{
"keypair_id": v.KeypairId,
"name": v.Name,
"description": v.Description,
"created_time": v.CreatedTime,
"public_key": v.PublicKey,
"instance_count": v.InstanceCount,
"region_id": v.RegionId,
"fingerprint": v.FingerPrint,
}
tfList = append(tfList, tfMap)
}
return tfList
}
95 changes: 95 additions & 0 deletions baiducloud/service/bcc/data_source_key_pairs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package bcc

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
"log"
)

func DataSourceKeyPairs() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to query BCC key pairs. \n\n",

Read: dataSourceKeyPairsRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of key pair. Use this to filter key pair list.",
Optional: true,
},
"key_pairs": {
Type: schema.TypeList,
Description: "The key pair list.",
Computed: true,
Elem: KeyPairSchema(),
},
},
}
}

func KeyPairSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"keypair_id": {
Type: schema.TypeString,
Description: "The id of key pair.",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "The name of key pair.",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "The description of key pair.",
Computed: true,
},
"created_time": {
Type: schema.TypeString,
Description: "The creation time of key pair.",
Computed: true,
},
"public_key": {
Type: schema.TypeString,
Description: "The public key of keypair.",
Computed: true,
},
"instance_count": {
Type: schema.TypeInt,
Description: "The number of instances bound to key pair.",
Computed: true,
},
"region_id": {
Type: schema.TypeString,
Description: "The id of the region to which key pair belongs.",
Computed: true,
},
"fingerprint": {
Type: schema.TypeString,
Description: "The fingerprint of key pair.",
Computed: true,
},
},
}
}

func dataSourceKeyPairsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*connectivity.BaiduClient)
keyPairs, err := FindKeyPairs(conn, d.Get("name").(string))

log.Printf("[DEBUG] Read BCC key pairs result: %+v", keyPairs)
if err != nil {
return fmt.Errorf("error reading BCC key pairs: %w", err)
}

if err := d.Set("key_pairs", flattenKeyPairList(keyPairs)); err != nil {
return fmt.Errorf("error setting key_pairs: %w", err)
}

d.SetId(resource.UniqueId())
return nil
}
31 changes: 31 additions & 0 deletions baiducloud/service/bcc/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package bcc

import (
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)

func FindKeyPairs(conn *connectivity.BaiduClient, name string) ([]api.KeypairModel, error) {
keyPairs := make([]api.KeypairModel, 0)
args := &api.ListKeypairArgs{Name: name}
for {
raw, err := conn.WithBccClient(func(client *bcc.Client) (interface{}, error) {
return client.ListKeypairs(args)
})
if err != nil {
return nil, err
}

result := raw.(*api.ListKeypairResult)
for _, item := range result.Keypairs {
keyPairs = append(keyPairs, item)
}

if !result.IsTruncated {
break
}
args.Marker = result.NextMarker
}
return keyPairs, nil
}
5 changes: 5 additions & 0 deletions examples/data-sources/baiducloud_bcc_key_pairs/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "baiducloud_bcc_key_pairs" "example" {

name = "key_pair_name"

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/terraform-providers/terraform-provider-baiducloud

require (
github.com/baidubce/bce-sdk-go v0.9.153
github.com/baidubce/bce-sdk-go v0.9.155
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/mitchellh/go-homedir v1.1.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY
github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3ATZkfNZeM=
github.com/aws/aws-sdk-go v1.37.0 h1:GzFnhOIsrGyQ69s7VgqtrG2BG8v7X7vwB3Xpbd/DBBk=
github.com/aws/aws-sdk-go v1.37.0/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/baidubce/bce-sdk-go v0.9.153 h1:h5l2EXehe4C4/bdlAPBaULrbnEDgIu5HOYgniN7bjGM=
github.com/baidubce/bce-sdk-go v0.9.153/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg=
github.com/baidubce/bce-sdk-go v0.9.155 h1:4TeNC0+NSOW5h5+nmZLSKquZsC1WnX5n2ohxxH8/iWg=
github.com/baidubce/bce-sdk-go v0.9.155/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/baidubce/bce-sdk-go/bce/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 60 additions & 3 deletions vendor/github.com/baidubce/bce-sdk-go/services/bos/api/bucket.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d922c8

Please sign in to comment.