github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/rds.go (about)

     1  // Copyright 2018 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 alicloud
    16  
    17  import (
    18  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    19  	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
    20  	"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
    21  )
    22  
    23  // RdsGenerator Struct for generating AliCloud Elastic Compute Service
    24  type RdsGenerator struct {
    25  	AliCloudService
    26  }
    27  
    28  func resourceFromrdsResponse(rds rds.DBInstance) terraformutils.Resource {
    29  	return terraformutils.NewResource(
    30  		rds.DBInstanceId, // nolint
    31  		rds.DBInstanceId+"__"+rds.DBInstanceDescription, // nolint
    32  		"alicloud_db_instance",
    33  		"alicloud",
    34  		map[string]string{},
    35  		[]string{},
    36  		map[string]interface{}{},
    37  	)
    38  }
    39  
    40  // InitResources Gets the list of all rds ids and generates resources
    41  func (g *RdsGenerator) InitResources() error {
    42  	client, err := g.LoadClientFromProfile()
    43  	if err != nil {
    44  		return err
    45  	}
    46  	remaining := 1
    47  	pageNumber := 1
    48  	pageSize := 10
    49  
    50  	allrdss := make([]rds.DBInstance, 0)
    51  
    52  	for remaining > 0 {
    53  		raw, err := client.WithRdsClient(func(rdsClient *rds.Client) (interface{}, error) {
    54  			request := rds.CreateDescribeDBInstancesRequest()
    55  			request.RegionId = client.RegionID
    56  			request.PageSize = requests.NewInteger(pageSize)
    57  			request.PageNumber = requests.NewInteger(pageNumber)
    58  			return rdsClient.DescribeDBInstances(request)
    59  		})
    60  		if err != nil {
    61  			return err
    62  		}
    63  
    64  		response := raw.(*rds.DescribeDBInstancesResponse)
    65  		allrdss = append(allrdss, response.Items.DBInstance...)
    66  		remaining = response.TotalRecordCount - pageNumber*pageSize
    67  		pageNumber++
    68  	}
    69  
    70  	for _, rds := range allrdss {
    71  		resource := resourceFromrdsResponse(rds)
    72  		g.Resources = append(g.Resources, resource)
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  // PostConvertHook Runs before HCL files are generated
    79  func (g *RdsGenerator) PostConvertHook() error {
    80  	for _, r := range g.Resources {
    81  		if r.InstanceInfo.Type == "alicloud_db_instance" {
    82  			// https://www.terraform.io/docs/providers/alicloud/r/db_instance.html#period
    83  			if r.Item["instance_charge_type"] != "PrePaid" {
    84  				delete(r.Item, "period")
    85  			}
    86  		}
    87  	}
    88  
    89  	return nil
    90  }