github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/tencentcloud/cos.go (about)

     1  // Copyright 2021 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tencentcloud
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/http"
    21  	"net/url"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
    25  	"github.com/tencentyun/cos-go-sdk-v5"
    26  )
    27  
    28  type CosGenerator struct {
    29  	TencentCloudService
    30  }
    31  
    32  func (g *CosGenerator) InitResources() error {
    33  	args := g.GetArgs()
    34  	region := args["region"].(string)
    35  	credential := args["credential"].(common.Credential)
    36  	requestURL := fmt.Sprintf("https://cos.%s.myqcloud.com", region)
    37  	u, _ := url.Parse(requestURL)
    38  	uri := &cos.BaseURL{ServiceURL: u}
    39  	client := cos.NewClient(uri, &http.Client{
    40  		Transport: &cos.AuthorizationTransport{
    41  			SecretID:     credential.SecretId,
    42  			SecretKey:    credential.SecretKey,
    43  			SessionToken: credential.Token,
    44  		},
    45  	})
    46  
    47  	result, _, err := client.Service.Get(context.Background())
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	for _, bucket := range result.Buckets {
    53  		resource := terraformutils.NewResource(
    54  			bucket.Name,
    55  			bucket.Name,
    56  			"tencentcloud_cos_bucket",
    57  			"tencentcloud",
    58  			map[string]string{},
    59  			[]string{},
    60  			map[string]interface{}{},
    61  		)
    62  		g.Resources = append(g.Resources, resource)
    63  	}
    64  
    65  	return nil
    66  }