github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/ram.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/providers/alicloud/connectivity"
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
    23  )
    24  
    25  // RAMGenerator Struct for generating AliCloud Elastic Compute Service
    26  type RAMGenerator struct {
    27  	AliCloudService
    28  }
    29  
    30  func resourceFromRAMRole(role ram.RoleInListRoles) terraformutils.Resource {
    31  	return terraformutils.NewResource(
    32  		role.RoleName,                  // id
    33  		role.RoleId+"__"+role.RoleName, // name
    34  		"alicloud_ram_role",
    35  		"alicloud",
    36  		map[string]string{},
    37  		[]string{},
    38  		map[string]interface{}{},
    39  	)
    40  }
    41  
    42  func resourceFromRAMPolicy(policy ram.PolicyInListPoliciesForRole, roleName string) terraformutils.Resource {
    43  	// https://github.com/terraform-providers/terraform-provider-alicloud/blob/master/alicloud/resource_alicloud_ram_role_policy_attachment.go#L93
    44  	id := strings.Join([]string{"role", policy.PolicyName, policy.PolicyType, roleName}, ":")
    45  
    46  	return terraformutils.NewResource(
    47  		id, // id
    48  		id+"__"+roleName+"_"+policy.PolicyName, // name
    49  		"alicloud_ram_role_policy_attachment",
    50  		"alicloud",
    51  		map[string]string{},
    52  		[]string{},
    53  		map[string]interface{}{},
    54  	)
    55  }
    56  
    57  func initRoles(client *connectivity.AliyunClient) ([]ram.RoleInListRoles, error) {
    58  	allRoles := make([]ram.RoleInListRoles, 0)
    59  
    60  	raw, err := client.WithRAMClient(func(ramClient *ram.Client) (interface{}, error) {
    61  		request := ram.CreateListRolesRequest()
    62  		request.RegionId = client.RegionID
    63  		return ramClient.ListRoles(request)
    64  	})
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	response := raw.(*ram.ListRolesResponse)
    70  	allRoles = append(allRoles, response.Roles.Role...)
    71  
    72  	return allRoles, nil
    73  }
    74  
    75  func initRAMPolicyAttachment(client *connectivity.AliyunClient, allRoles []ram.RoleInListRoles) ([]ram.PolicyInListPoliciesForRole, []string, error) {
    76  	allRAMPolicies := make([]ram.PolicyInListPoliciesForRole, 0)
    77  	roleNames := make([]string, 0)
    78  
    79  	for _, role := range allRoles {
    80  		raw, err := client.WithRAMClient(func(ramClient *ram.Client) (interface{}, error) {
    81  			request := ram.CreateListPoliciesForRoleRequest()
    82  			request.RegionId = client.RegionID
    83  			request.RoleName = role.RoleName
    84  			return ramClient.ListPoliciesForRole(request)
    85  		})
    86  		if err != nil {
    87  			return nil, nil, err
    88  		}
    89  
    90  		response := raw.(*ram.ListPoliciesForRoleResponse)
    91  		for _, policy := range response.Policies.Policy {
    92  			allRAMPolicies = append(allRAMPolicies, policy)
    93  			roleNames = append(roleNames, role.RoleName)
    94  		}
    95  	}
    96  
    97  	return allRAMPolicies, roleNames, nil
    98  }
    99  
   100  // InitResources Gets the list of all ram role ids and generates resources
   101  func (g *RAMGenerator) InitResources() error {
   102  	client, err := g.LoadClientFromProfile()
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	allRoles, err := initRoles(client)
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	allRAMPolicyAttachment, roleNames, err := initRAMPolicyAttachment(client, allRoles)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	for _, role := range allRoles {
   118  		resource := resourceFromRAMRole(role)
   119  		g.Resources = append(g.Resources, resource)
   120  	}
   121  
   122  	for i, ramPolicy := range allRAMPolicyAttachment {
   123  		resource := resourceFromRAMPolicy(ramPolicy, roleNames[i])
   124  		g.Resources = append(g.Resources, resource)
   125  	}
   126  
   127  	return nil
   128  }
   129  
   130  // PostConvertHook Runs before HCL files are generated
   131  func (g *RAMGenerator) PostConvertHook() error {
   132  	for _, r := range g.Resources {
   133  		if r.InstanceInfo.Type == "alicloud_ram_role" {
   134  			// https://www.terraform.io/docs/providers/alicloud/r/ram_role.html
   135  			delete(r.Item, "services")  // deprecated
   136  			delete(r.Item, "ram_users") // deprecated
   137  			delete(r.Item, "version")   // deprecated
   138  		}
   139  	}
   140  
   141  	return nil
   142  }