-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
- Loading branch information
There are no files selected for viewing
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 | ||
} |
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 | ||
} |
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
data "baiducloud_bcc_key_pairs" "example" { | ||
|
||
name = "key_pair_name" | ||
|
||
} |
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.
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.