github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/opsworks.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 aws
    16  
    17  import (
    18  	"context"
    19  	"github.com/aws/aws-sdk-go-v2/service/opsworks"
    20  	"github.com/aws/aws-sdk-go-v2/service/opsworks/types"
    21  	"log"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  )
    25  
    26  type OpsworksGenerator struct {
    27  	AWSService
    28  }
    29  
    30  func (g *OpsworksGenerator) InitResources() error {
    31  	config, e := g.generateConfig()
    32  	if e != nil {
    33  		return e
    34  	}
    35  	svc := opsworks.NewFromConfig(config)
    36  
    37  	e = g.fetchStacks(svc)
    38  	if e != nil {
    39  		return e
    40  	}
    41  
    42  	e = g.fetchUserProfile(svc)
    43  	if e != nil {
    44  		return e
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func (g *OpsworksGenerator) fetchApps(stackID *string, svc *opsworks.Client) error {
    51  	apps, err := svc.DescribeApps(context.TODO(), &opsworks.DescribeAppsInput{
    52  		StackId: stackID,
    53  	})
    54  	if err != nil {
    55  		return err
    56  	}
    57  	for _, app := range apps.Apps {
    58  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    59  			StringValue(app.AppId),
    60  			StringValue(app.AppId),
    61  			"aws_opsworks_application",
    62  			"aws",
    63  			[]string{"tags."},
    64  		))
    65  	}
    66  	return nil
    67  }
    68  
    69  func (g *OpsworksGenerator) fetchLayers(stackID *string, svc *opsworks.Client) error {
    70  	apps, err := svc.DescribeLayers(context.TODO(), &opsworks.DescribeLayersInput{
    71  		StackId: stackID,
    72  	})
    73  	if err != nil {
    74  		return err
    75  	}
    76  	for _, layer := range apps.Layers {
    77  		switch layer.Type {
    78  		case types.LayerTypeCustom:
    79  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    80  				StringValue(layer.LayerId),
    81  				StringValue(layer.LayerId),
    82  				"aws_opsworks_custom_layer",
    83  				"aws",
    84  				[]string{"tags."},
    85  			))
    86  		case types.LayerTypePhpApp:
    87  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    88  				StringValue(layer.LayerId),
    89  				StringValue(layer.LayerId),
    90  				"aws_opsworks_php_app_layer",
    91  				"aws",
    92  				[]string{"tags."},
    93  			))
    94  		case types.LayerTypeJavaApp:
    95  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    96  				StringValue(layer.LayerId),
    97  				StringValue(layer.LayerId),
    98  				"aws_opsworks_java_app_layer",
    99  				"aws",
   100  				[]string{"tags."},
   101  			))
   102  		case types.LayerTypeWeb:
   103  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   104  				StringValue(layer.LayerId),
   105  				StringValue(layer.LayerId),
   106  				"aws_opsworks_static_web_layer",
   107  				"aws",
   108  				[]string{"tags."},
   109  			))
   110  		}
   111  	}
   112  	return nil
   113  }
   114  
   115  func (g *OpsworksGenerator) fetchInstances(stackID *string, svc *opsworks.Client) error {
   116  	apps, err := svc.DescribeInstances(context.TODO(), &opsworks.DescribeInstancesInput{
   117  		StackId: stackID,
   118  	})
   119  	if err != nil {
   120  		return err
   121  	}
   122  	for _, instances := range apps.Instances {
   123  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   124  			StringValue(instances.InstanceId),
   125  			StringValue(instances.InstanceId),
   126  			"aws_opsworks_instance",
   127  			"aws",
   128  			[]string{"tags."},
   129  		))
   130  	}
   131  	return nil
   132  }
   133  func (g *OpsworksGenerator) fetchRdsInstances(stackID *string, svc *opsworks.Client) error {
   134  	apps, err := svc.DescribeRdsDbInstances(context.TODO(), &opsworks.DescribeRdsDbInstancesInput{
   135  		StackId: stackID,
   136  	})
   137  	if err != nil {
   138  		return err
   139  	}
   140  	for _, rdsDbInstance := range apps.RdsDbInstances {
   141  		g.Resources = append(g.Resources, terraformutils.NewResource(
   142  			StringValue(rdsDbInstance.RdsDbInstanceArn),
   143  			StringValue(rdsDbInstance.RdsDbInstanceArn),
   144  			"aws_opsworks_instance",
   145  			"aws",
   146  			map[string]string{
   147  				"rds_db_instance_arn": StringValue(rdsDbInstance.RdsDbInstanceArn),
   148  				"stack_id":            StringValue(stackID),
   149  			},
   150  			[]string{"tags."},
   151  			map[string]interface{}{},
   152  		))
   153  	}
   154  	return nil
   155  }
   156  
   157  func (g *OpsworksGenerator) fetchStacks(svc *opsworks.Client) error {
   158  	apps, err := svc.DescribeStacks(context.TODO(), &opsworks.DescribeStacksInput{})
   159  	if err != nil {
   160  		return err
   161  	}
   162  	for _, stack := range apps.Stacks {
   163  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   164  			StringValue(stack.StackId),
   165  			StringValue(stack.StackId),
   166  			"aws_opsworks_stack",
   167  			"aws",
   168  			[]string{"tags."},
   169  		))
   170  
   171  		e := g.fetchApps(stack.StackId, svc)
   172  		if e != nil {
   173  			log.Println(err)
   174  		}
   175  
   176  		e = g.fetchInstances(stack.StackId, svc)
   177  		if e != nil {
   178  			log.Println(err)
   179  		}
   180  
   181  		e = g.fetchRdsInstances(stack.StackId, svc)
   182  		if e != nil {
   183  			log.Println(err)
   184  		}
   185  
   186  		e = g.fetchLayers(stack.StackId, svc)
   187  		if e != nil {
   188  			log.Println(err)
   189  		}
   190  	}
   191  	return nil
   192  }
   193  
   194  func (g *OpsworksGenerator) fetchUserProfile(svc *opsworks.Client) error {
   195  	apps, err := svc.DescribeUserProfiles(context.TODO(), &opsworks.DescribeUserProfilesInput{})
   196  	if err != nil {
   197  		return err
   198  	}
   199  	for _, userProfile := range apps.UserProfiles {
   200  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   201  			StringValue(userProfile.IamUserArn),
   202  			StringValue(userProfile.IamUserArn),
   203  			"aws_opsworks_user_profile",
   204  			"aws",
   205  			[]string{"tags."},
   206  		))
   207  	}
   208  	return nil
   209  }