github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/ecs.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  	"strings"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
    22  	"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
    23  )
    24  
    25  // EcsGenerator Struct for generating AliCloud Elastic Compute Service
    26  type EcsGenerator struct {
    27  	AliCloudService
    28  }
    29  
    30  func resourceFromInstance(instance ecs.Instance) terraformutils.Resource {
    31  	return terraformutils.NewResource(
    32  		instance.InstanceId, // id
    33  		instance.InstanceId+"__"+instance.InstanceName, // name
    34  		"alicloud_instance",
    35  		"alicloud",
    36  		map[string]string{},
    37  		[]string{},
    38  		map[string]interface{}{},
    39  	)
    40  }
    41  
    42  // InitResources Gets the list of all ECS instance ids and generates resources
    43  func (g *EcsGenerator) InitResources() error {
    44  	client, err := g.LoadClientFromProfile()
    45  	if err != nil {
    46  		return err
    47  	}
    48  	var filters []ecs.DescribeInstancesTag
    49  	for _, filter := range g.Filter {
    50  		if strings.HasPrefix(filter.FieldPath, "tags.") {
    51  			filters = append(filters, ecs.DescribeInstancesTag{
    52  				Key:   strings.TrimPrefix(filter.FieldPath, "tags."),
    53  				Value: filter.AcceptableValues[0],
    54  			})
    55  		}
    56  	}
    57  
    58  	remaining := 1
    59  	pageNumber := 1
    60  	pageSize := 10
    61  
    62  	allInstances := make([]ecs.Instance, 0)
    63  
    64  	for remaining > 0 {
    65  		raw, err := client.WithEcsClient(func(ecsClient *ecs.Client) (interface{}, error) {
    66  			request := ecs.CreateDescribeInstancesRequest()
    67  			request.Tag = &filters
    68  			request.RegionId = client.RegionID
    69  			request.PageSize = requests.NewInteger(pageSize)
    70  			request.PageNumber = requests.NewInteger(pageNumber)
    71  			return ecsClient.DescribeInstances(request)
    72  		})
    73  		if err != nil {
    74  			return err
    75  		}
    76  
    77  		response := raw.(*ecs.DescribeInstancesResponse)
    78  		allInstances = append(allInstances, response.Instances.Instance...)
    79  		remaining = response.TotalCount - pageNumber*pageSize
    80  		pageNumber++
    81  	}
    82  
    83  	for _, instance := range allInstances {
    84  		resource := resourceFromInstance(instance)
    85  		g.Resources = append(g.Resources, resource)
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // PostConvertHook Runs before HCL files are generated
    92  func (g *EcsGenerator) PostConvertHook() error {
    93  	for _, r := range g.Resources {
    94  		if r.InstanceInfo.Type == "alicloud_instance" {
    95  			// subnet_id is absent in the documentation
    96  			// https://www.terraform.io/docs/providers/alicloud/r/instance.html
    97  			delete(r.Item, "subnet_id")
    98  		}
    99  	}
   100  
   101  	return nil
   102  }