github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/batch.go (about)

     1  package aws
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
     7  
     8  	"github.com/aws/aws-sdk-go-v2/aws"
     9  	"github.com/aws/aws-sdk-go-v2/service/batch"
    10  )
    11  
    12  var BatchAllowEmptyValues = []string{"tags."}
    13  
    14  var BatchAdditionalFields = map[string]interface{}{}
    15  
    16  type BatchGenerator struct {
    17  	AWSService
    18  }
    19  
    20  func (g *BatchGenerator) InitResources() error {
    21  	config, e := g.generateConfig()
    22  	if e != nil {
    23  		return e
    24  	}
    25  	batchClient := batch.NewFromConfig(config)
    26  
    27  	if err := g.loadComputeEnvironments(batchClient); err != nil {
    28  		return err
    29  	}
    30  	if err := g.loadJobDefinitions(batchClient); err != nil {
    31  		return err
    32  	}
    33  	if err := g.loadJobQueues(batchClient); err != nil {
    34  		return err
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func (g *BatchGenerator) loadComputeEnvironments(batchClient *batch.Client) error {
    41  	p := batch.NewDescribeComputeEnvironmentsPaginator(batchClient, &batch.DescribeComputeEnvironmentsInput{})
    42  	for p.HasMorePages() {
    43  		page, err := p.NextPage(context.TODO())
    44  		if err != nil {
    45  			return err
    46  		}
    47  		for _, computeEnvironment := range page.ComputeEnvironments {
    48  			computeEnvironmentName := StringValue(computeEnvironment.ComputeEnvironmentName)
    49  			g.Resources = append(g.Resources, terraformutils.NewResource(
    50  				computeEnvironmentName,
    51  				computeEnvironmentName,
    52  				"aws_batch_compute_environment",
    53  				"aws",
    54  				map[string]string{
    55  					"compute_environment_name": computeEnvironmentName,
    56  				},
    57  				BatchAllowEmptyValues,
    58  				BatchAdditionalFields,
    59  			))
    60  		}
    61  	}
    62  	return nil
    63  }
    64  
    65  func (g *BatchGenerator) loadJobDefinitions(batchClient *batch.Client) error {
    66  	p := batch.NewDescribeJobDefinitionsPaginator(batchClient, &batch.DescribeJobDefinitionsInput{
    67  		Status: aws.String("ACTIVE"),
    68  	})
    69  	for p.HasMorePages() {
    70  		page, err := p.NextPage(context.TODO())
    71  		if err != nil {
    72  			return err
    73  		}
    74  		for _, jobDefinition := range page.JobDefinitions {
    75  			jobDefinitionName := StringValue(jobDefinition.JobDefinitionName)
    76  			g.Resources = append(g.Resources, terraformutils.NewResource(
    77  				jobDefinitionName,
    78  				jobDefinitionName,
    79  				"aws_batch_job_definition",
    80  				"aws",
    81  				map[string]string{
    82  					"arn": StringValue(jobDefinition.JobDefinitionArn),
    83  				},
    84  				BatchAllowEmptyValues,
    85  				BatchAdditionalFields,
    86  			))
    87  		}
    88  	}
    89  	return nil
    90  }
    91  
    92  func (g *BatchGenerator) loadJobQueues(batchClient *batch.Client) error {
    93  	p := batch.NewDescribeJobQueuesPaginator(batchClient, &batch.DescribeJobQueuesInput{})
    94  	for p.HasMorePages() {
    95  		page, err := p.NextPage(context.TODO())
    96  		if err != nil {
    97  			return err
    98  		}
    99  		for _, jobQueue := range page.JobQueues {
   100  			jobQueueName := StringValue(jobQueue.JobQueueName)
   101  			g.Resources = append(g.Resources, terraformutils.NewResource(
   102  				jobQueueName,
   103  				jobQueueName,
   104  				"aws_batch_job_queue",
   105  				"aws",
   106  				map[string]string{},
   107  				BatchAllowEmptyValues,
   108  				BatchAdditionalFields,
   109  			))
   110  		}
   111  	}
   112  	return nil
   113  }