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

Support BBC and http proxy #12

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
53 changes: 53 additions & 0 deletions baiducloud/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package baiducloud

import (
"github.com/baidubce/bce-sdk-go/services/bbc"
"github.com/hashicorp/terraform/helper/schema"
)

func setBilling(d *schema.ResourceData, paymentTiming string) {
billingMap := map[string]interface{}{"payment_timing": paymentTiming}
billings := []interface{}{}
billings = append(billings, billingMap)
d.Set("billing", billings)
}

func createBillingSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"payment_timing": {
Type: schema.TypeString,
Description: "Payment timing of billing, which can be Prepaid or Postpaid. The default is Postpaid.",
Optional: true,
Default: bbc.PaymentTimingPostPaid,
ValidateFunc: validatePaymentTiming(),
},
"reservation": {
Type: schema.TypeMap,
Description: "Reservation of the instance.",
Optional: true,
//DiffSuppressFunc: postPaidDiffSuppressFunc,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"reservation_length": {
Type: schema.TypeInt,
Description: "The reservation length that you will pay for your resource. It is valid when payment_timing is Prepaid. Valid values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 24, 36].",
Required: true,
Default: 1,
ValidateFunc: validateReservationLength(),
//DiffSuppressFunc: postPaidDiffSuppressFunc,
},
"reservation_time_unit": {
Type: schema.TypeString,
Description: "The reservation time unit that you will pay for your resource. It is valid when payment_timing is Prepaid. The value can only be month currently, which is also the default value.",
Required: true,
Default: "Month",
ValidateFunc: validateReservationUnit(),
//DiffSuppressFunc: postPaidDiffSuppressFunc,
},
},
},
},
},
}
}
41 changes: 41 additions & 0 deletions baiducloud/connectivity/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package connectivity

import (
"os"
"sync"

"github.com/baidubce/bce-sdk-go/auth"
"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/services/appblb"
"github.com/baidubce/bce-sdk-go/services/bbc"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bos"
"github.com/baidubce/bce-sdk-go/services/cce"
Expand All @@ -30,6 +33,7 @@ type BaiduClient struct {

Credentials *auth.BceCredentials

bbcConn *bbc.Client
bccConn *bcc.Client
vpcConn *vpc.Client
eipConn *eip.Client
Expand Down Expand Up @@ -115,6 +119,30 @@ func (client *BaiduClient) WithCommonClient(serviceCode ServiceCode) *BaiduClien

return client
}
func (client *BaiduClient) WithProxy(config *bce.BceClientConfiguration) {
proxyUrl := os.Getenv("HTTP_PROXY")
if len(proxyUrl) > 0 {
config.ProxyUrl = proxyUrl
}
}
func (client *BaiduClient) WithBbcClient(do func(*bbc.Client) (interface{}, error)) (interface{}, error) {
goSdkMutex.Lock()
defer goSdkMutex.Unlock()

// Initialize the BBC client if necessary
if client.bbcConn == nil {
client.WithCommonClient(BBCCode)
bbcClient, err := bbc.NewClient(client.Credentials.AccessKeyId, client.Credentials.SecretAccessKey, client.Endpoint)
if err != nil {
return nil, err
}
client.WithProxy(bbcClient.Config)
bbcClient.Config.Credentials = client.Credentials
client.bbcConn = bbcClient
}

return do(client.bbcConn)
}

func (client *BaiduClient) WithBccClient(do func(*bcc.Client) (interface{}, error)) (interface{}, error) {
goSdkMutex.Lock()
Expand All @@ -127,6 +155,7 @@ func (client *BaiduClient) WithBccClient(do func(*bcc.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(bccClient.Config)
bccClient.Config.Credentials = client.Credentials

client.bccConn = bccClient
Expand All @@ -146,6 +175,7 @@ func (client *BaiduClient) WithVpcClient(do func(*vpc.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(vpcClient.Config)
vpcClient.Config.Credentials = client.Credentials

client.vpcConn = vpcClient
Expand All @@ -165,6 +195,7 @@ func (client *BaiduClient) WithEipClient(do func(*eip.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(eipClient.Config)
eipClient.Config.Credentials = client.Credentials

client.eipConn = eipClient
Expand All @@ -184,6 +215,7 @@ func (client *BaiduClient) WithAppBLBClient(do func(*appblb.Client) (interface{}
if err != nil {
return nil, err
}
client.WithProxy(appBlbClient.Config)
appBlbClient.Config.Credentials = client.Credentials

client.appBlbConn = appBlbClient
Expand All @@ -203,6 +235,7 @@ func (client *BaiduClient) WithBosClient(do func(*bos.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(bosClient.Config)
bosClient.Config.Credentials = client.Credentials

client.bosConn = bosClient
Expand All @@ -222,6 +255,7 @@ func (client *BaiduClient) WithCertClient(do func(*cert.Client) (interface{}, er
if err != nil {
return nil, err
}
client.WithProxy(certClient.Config)
certClient.Config.Credentials = client.Credentials

client.certConn = certClient
Expand All @@ -241,6 +275,7 @@ func (client *BaiduClient) WithCFCClient(do func(*cfc.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(cfcClient.Config)
cfcClient.Config.Credentials = client.Credentials

client.cfcConn = cfcClient
Expand All @@ -260,6 +295,7 @@ func (client *BaiduClient) WithScsClient(do func(*scs.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(scsClient.Config)
scsClient.Config.Credentials = client.Credentials

client.scsConn = scsClient
Expand All @@ -279,6 +315,7 @@ func (client *BaiduClient) WithCCEClient(do func(*cce.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(cceClient.Config)
cceClient.Config.Credentials = client.Credentials

client.cceConn = cceClient
Expand All @@ -298,6 +335,7 @@ func (client *BaiduClient) WithCCEv2Client(do func(*ccev2.Client) (interface{},
if err != nil {
return nil, err
}
client.WithProxy(ccev2Client.Config)
ccev2Client.Config.Credentials = client.Credentials

client.ccev2Conn = ccev2Client
Expand All @@ -317,6 +355,7 @@ func (client *BaiduClient) WithRdsClient(do func(*rds.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(rdsClient.Config)
rdsClient.Config.Credentials = client.Credentials

client.rdsConn = rdsClient
Expand All @@ -337,6 +376,7 @@ func (client *BaiduClient) WithDtsClient(do func(*dts.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(dtsClient.Config)
client.dtsConn = dtsClient
}

Expand All @@ -355,6 +395,7 @@ func (client *BaiduClient) WithIamClient(do func(*iam.Client) (interface{}, erro
if err != nil {
return nil, err
}
client.WithProxy(iamClient.Config)
client.iamConn = iamClient
}

Expand Down
2 changes: 1 addition & 1 deletion baiducloud/connectivity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Config struct {
AccessKey string
SecretKey string
Region Region

// assume role
AssumeRoleRoleName string
AssumeRoleAccountId string
Expand Down
1 change: 1 addition & 0 deletions baiducloud/connectivity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type ServiceCode string

const (
BBCCode = ServiceCode("BBC")
BCCCode = ServiceCode("BCC")
VPCCode = ServiceCode("VPC")
EIPCode = ServiceCode("EIP")
Expand Down
124 changes: 124 additions & 0 deletions baiducloud/data_source_baiducloud_bbc_flavors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Use this data source to query bbc flavors list.

Example Usage

```hcl
data "baiducloud_bbc_flavors" "default" {}

output "flavors" {
value = "${data.baiducloud_bbc_flavors.default.flavors}"
}
```
*/
package baiducloud

import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"

"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)

func dataSourceBaiduCloudBbcFlavors() *schema.Resource {
return &schema.Resource{
Read: dataSourceBaiduCloudBbcFlavorRead,

Schema: map[string]*schema.Schema{
"flavor_id": {
Type: schema.TypeString,
Description: " flavor id",
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Description: " flavors result output file",
Optional: true,
ForceNew: true,
},
"filter": dataSourceFiltersSchema(),

"flavors": {
Type: schema.TypeList,
Description: "flavor list",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flavor_id": {
Type: schema.TypeString,
Description: "flavor id",
Computed: true,
},
"cpu_count": {
Type: schema.TypeInt,
Description: "cpu count",
Computed: true,
},
"cpu_type": {
Type: schema.TypeString,
Description: "cpu type",
Computed: true,
},
"memory_capacity_in_gb": {
Type: schema.TypeInt,
Description: "memory size in gb",
Computed: true,
},
"disk": {
Type: schema.TypeString,
Description: "disk description",
Computed: true,
},
"network_card": {
Type: schema.TypeString,
Description: "network card",
Computed: true,
},
"os_name": {
Type: schema.TypeString,
Description: "Image os name",
Computed: true,
},
},
},
},
},
}
}

func dataSourceBaiduCloudBbcFlavorRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)
bbcService := BbcService{client}
action := "Query All Flavors"
flavorMap := make([]map[string]interface{}, 0)
if v, ok := d.GetOk("flavor_id"); ok {
result, err := bbcService.GetFlavorDetail(v.(string))
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_bbc_flavors", action, BCESDKGoERROR)
}
flavorMap = bbcService.FlattenFlavorDetailToMap(result)
} else {
result, err := bbcService.GetFlavors()
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_bbc_flavors", action, BCESDKGoERROR)
}
flavorMap = bbcService.FlattenFlavorsToMap(result.Flavors)
}

addDebug(action, flavorMap)

FilterDataSourceResult(d, &flavorMap)

if err := d.Set("flavors", flavorMap); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_bbc_flavors", action, BCESDKGoERROR)
}
d.SetId(resource.UniqueId())
if v, ok := d.GetOk("output_file"); ok && v.(string) != "" {
if err := writeToFile(v.(string), flavorMap); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_bbc_flavors", action, BCESDKGoERROR)
}
}

return nil
}
Loading