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

     1  // Copyright 2020 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  	"fmt"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  
    23  	"github.com/aws/aws-sdk-go-v2/service/efs"
    24  )
    25  
    26  var efsAllowEmptyValues = []string{"tags."}
    27  
    28  type EfsGenerator struct {
    29  	AWSService
    30  }
    31  
    32  func (g *EfsGenerator) InitResources() error {
    33  	config, e := g.generateConfig()
    34  	if e != nil {
    35  		return e
    36  	}
    37  	svc := efs.NewFromConfig(config)
    38  	if err := g.loadFileSystem(svc); err != nil {
    39  		return err
    40  	}
    41  	if err := g.loadAccessPoint(svc); err != nil {
    42  		return err
    43  	}
    44  	if err := g.loadMountTarget(svc); err != nil {
    45  		return err
    46  	}
    47  	return nil
    48  }
    49  
    50  func (g *EfsGenerator) loadFileSystem(svc *efs.Client) error {
    51  	p := efs.NewDescribeFileSystemsPaginator(svc, &efs.DescribeFileSystemsInput{})
    52  	for p.HasMorePages() {
    53  		page, err := p.NextPage(context.TODO())
    54  		if err != nil {
    55  			return err
    56  		}
    57  		for _, fileSystem := range page.FileSystems {
    58  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    59  				StringValue(fileSystem.FileSystemId),
    60  				StringValue(fileSystem.FileSystemId),
    61  				"aws_efs_file_system",
    62  				"aws",
    63  				efsAllowEmptyValues))
    64  
    65  			targetsResponse, err := svc.DescribeMountTargets(context.TODO(), &efs.DescribeMountTargetsInput{
    66  				FileSystemId: fileSystem.FileSystemId,
    67  			})
    68  			if err != nil {
    69  				fmt.Println(err.Error())
    70  				continue
    71  			}
    72  			for _, mountTarget := range targetsResponse.MountTargets {
    73  				g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    74  					StringValue(mountTarget.MountTargetId),
    75  					StringValue(mountTarget.MountTargetId),
    76  					"aws_efs_mount_target",
    77  					"aws",
    78  					efsAllowEmptyValues))
    79  			}
    80  
    81  			policyResponse, err := svc.DescribeFileSystemPolicy(context.TODO(), &efs.DescribeFileSystemPolicyInput{
    82  				FileSystemId: fileSystem.FileSystemId,
    83  			})
    84  			if err != nil {
    85  				fmt.Println(err.Error())
    86  				continue
    87  			}
    88  			escapedPolicy := g.escapeAwsInterpolation(StringValue(policyResponse.Policy))
    89  			g.Resources = append(g.Resources, terraformutils.NewResource(
    90  				StringValue(fileSystem.FileSystemId),
    91  				StringValue(fileSystem.FileSystemId),
    92  				"aws_efs_file_system_policy",
    93  				"aws",
    94  				map[string]string{
    95  					"file_system_id": StringValue(fileSystem.FileSystemId),
    96  					"policy": fmt.Sprintf(`<<POLICY
    97  %s
    98  POLICY`, escapedPolicy),
    99  				},
   100  				efsAllowEmptyValues,
   101  				map[string]interface{}{}))
   102  		}
   103  	}
   104  	return nil
   105  }
   106  
   107  func (g *EfsGenerator) loadMountTarget(svc *efs.Client) error {
   108  	p := efs.NewDescribeFileSystemsPaginator(svc, &efs.DescribeFileSystemsInput{})
   109  	for p.HasMorePages() {
   110  		page, err := p.NextPage(context.TODO())
   111  		if err != nil {
   112  			return err
   113  		}
   114  		for _, fileSystem := range page.FileSystems {
   115  			id := StringValue(fileSystem.FileSystemId)
   116  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   117  				id,
   118  				id,
   119  				"aws_efs_file_system",
   120  				"aws",
   121  				efsAllowEmptyValues))
   122  		}
   123  	}
   124  	return nil
   125  }
   126  
   127  func (g *EfsGenerator) loadAccessPoint(svc *efs.Client) error {
   128  	p := efs.NewDescribeAccessPointsPaginator(svc, &efs.DescribeAccessPointsInput{})
   129  	for p.HasMorePages() {
   130  		page, err := p.NextPage(context.TODO())
   131  		if err != nil {
   132  			return err
   133  		}
   134  		for _, fileSystem := range page.AccessPoints {
   135  			id := StringValue(fileSystem.AccessPointId)
   136  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
   137  				id,
   138  				id,
   139  				"aws_efs_access_point",
   140  				"aws",
   141  				efsAllowEmptyValues))
   142  		}
   143  	}
   144  	return nil
   145  }