github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/alicloud_provider.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  	"errors"
    19  	"fmt"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/zclconf/go-cty/cty"
    23  )
    24  
    25  // AliCloudProvider Provider for alicloud
    26  type AliCloudProvider struct { //nolint
    27  	terraformutils.Provider
    28  	region  string
    29  	profile string
    30  }
    31  
    32  const GlobalRegion = "alicloud-global"
    33  
    34  // GetConfig Converts json config to go-cty
    35  func (p *AliCloudProvider) GetConfig() cty.Value {
    36  	profile := p.profile
    37  	config, err := LoadConfigFromProfile(profile)
    38  	if err != nil {
    39  		fmt.Println("ERROR:", err)
    40  	}
    41  
    42  	region := p.region
    43  	if region == "" {
    44  		region = config.RegionID
    45  	}
    46  
    47  	var val cty.Value
    48  	if config.RAMRoleArn != "" {
    49  		val = cty.ObjectVal(map[string]cty.Value{
    50  			"region":  cty.StringVal(region),
    51  			"profile": cty.StringVal(profile),
    52  			"assume_role": cty.SetVal([]cty.Value{
    53  				cty.ObjectVal(map[string]cty.Value{
    54  					"role_arn": cty.StringVal(config.RAMRoleArn),
    55  				}),
    56  			}),
    57  		})
    58  	} else {
    59  		val = cty.ObjectVal(map[string]cty.Value{
    60  			"region":  cty.StringVal(region),
    61  			"profile": cty.StringVal(profile),
    62  		})
    63  	}
    64  
    65  	return val
    66  }
    67  
    68  // GetResourceConnections Gets resource connections for alicloud
    69  func (p AliCloudProvider) GetResourceConnections() map[string]map[string][]string {
    70  	return map[string]map[string][]string{
    71  		// TODO: Not implemented
    72  	}
    73  }
    74  
    75  // GetProviderData Used for generated HCL2 for the provider
    76  func (p AliCloudProvider) GetProviderData(arg ...string) map[string]interface{} {
    77  	alicloudConfig := map[string]interface{}{}
    78  	if p.region == GlobalRegion {
    79  		alicloudConfig["region"] = "cn-hangzhou"
    80  	} else {
    81  		alicloudConfig["region"] = p.region
    82  	}
    83  	return map[string]interface{}{
    84  		"provider": map[string]interface{}{
    85  			"alicloud": alicloudConfig,
    86  		},
    87  	}
    88  }
    89  
    90  // Init Loads up command line arguments in the provider
    91  func (p *AliCloudProvider) Init(args []string) error {
    92  	p.region = args[0]
    93  	p.profile = args[1]
    94  	return nil
    95  }
    96  
    97  // GetName Gets name of provider
    98  func (p *AliCloudProvider) GetName() string {
    99  	return "alicloud"
   100  }
   101  
   102  // InitService Initializes the AliCloud service
   103  func (p *AliCloudProvider) InitService(serviceName string, verbose bool) error {
   104  	var isSupported bool
   105  	if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
   106  		return errors.New("alicloud: " + serviceName + " not supported service")
   107  	}
   108  	p.Service = p.GetSupportedService()[serviceName]
   109  	p.Service.SetName(serviceName)
   110  	p.Service.SetVerbose(verbose)
   111  	p.Service.SetProviderName(p.GetName())
   112  	p.Service.SetArgs(map[string]interface{}{
   113  		"region":  p.region,
   114  		"profile": p.profile,
   115  	})
   116  	return nil
   117  }
   118  
   119  // GetSupportedService Gets a list of all supported services
   120  func (p *AliCloudProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
   121  	return map[string]terraformutils.ServiceGenerator{
   122  		"dns":     &DNSGenerator{},
   123  		"ecs":     &EcsGenerator{},
   124  		"keypair": &KeyPairGenerator{},
   125  		"nat":     &NatGatewayGenerator{},
   126  		"pvtz":    &PvtzGenerator{},
   127  		"ram":     &RAMGenerator{},
   128  		"rds":     &RdsGenerator{},
   129  		"sg":      &SgGenerator{},
   130  		"slb":     &SlbGenerator{},
   131  		"vpc":     &VpcGenerator{},
   132  		"vswitch": &VSwitchGenerator{},
   133  	}
   134  }