github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/gcs.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 gcp
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"strconv"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  
    25  	"google.golang.org/api/storage/v1"
    26  )
    27  
    28  var GcsAllowEmptyValues = []string{"labels.", "created_before"}
    29  
    30  var GcsAdditionalFields = map[string]interface{}{}
    31  
    32  type GcsGenerator struct {
    33  	GCPService
    34  }
    35  
    36  func (g *GcsGenerator) createBucketsResources(ctx context.Context, gcsService *storage.Service) []terraformutils.Resource {
    37  	resources := []terraformutils.Resource{}
    38  	bucketList := gcsService.Buckets.List(g.GetArgs()["project"].(string))
    39  	if err := bucketList.Pages(ctx, func(page *storage.Buckets) error {
    40  		for _, bucket := range page.Items {
    41  			resources = append(resources, terraformutils.NewResource(
    42  				bucket.Name,
    43  				bucket.Name,
    44  				"google_storage_bucket",
    45  				g.ProviderName,
    46  				map[string]string{
    47  					"name":          bucket.Name,
    48  					"force_destroy": "false",
    49  				},
    50  				GcsAllowEmptyValues,
    51  				GcsAdditionalFields,
    52  			))
    53  			resources = append(resources, terraformutils.NewResource(
    54  				bucket.Name,
    55  				bucket.Name,
    56  				"google_storage_bucket_acl",
    57  				g.ProviderName,
    58  				map[string]string{
    59  					"bucket":        bucket.Name,
    60  					"role_entity.#": strconv.Itoa(len(bucket.Acl)),
    61  				},
    62  				GcsAllowEmptyValues,
    63  				GcsAdditionalFields,
    64  			))
    65  			resources = append(resources, terraformutils.NewResource(
    66  				bucket.Name,
    67  				bucket.Name,
    68  				"google_storage_default_object_acl",
    69  				g.ProviderName,
    70  				map[string]string{
    71  					"bucket":        bucket.Name,
    72  					"role_entity.#": strconv.Itoa(len(bucket.Acl)),
    73  				},
    74  				GcsAllowEmptyValues,
    75  				GcsAdditionalFields,
    76  			))
    77  			resources = append(resources, terraformutils.NewResource(
    78  				bucket.Name,
    79  				bucket.Name,
    80  				"google_storage_bucket_iam_binding",
    81  				g.ProviderName,
    82  				map[string]string{
    83  					"bucket": bucket.Name,
    84  				},
    85  				GcsAllowEmptyValues,
    86  				GcsAdditionalFields,
    87  			))
    88  			resources = append(resources, terraformutils.NewResource(
    89  				bucket.Name,
    90  				bucket.Name,
    91  				"google_storage_bucket_iam_member",
    92  				g.ProviderName,
    93  				map[string]string{
    94  					"bucket": bucket.Name,
    95  				},
    96  				GcsAllowEmptyValues,
    97  				GcsAdditionalFields,
    98  			))
    99  			resources = append(resources, terraformutils.NewResource(
   100  				bucket.Name,
   101  				bucket.Name,
   102  				"google_storage_bucket_iam_policy",
   103  				g.ProviderName,
   104  				map[string]string{
   105  					"bucket": bucket.Name,
   106  				},
   107  				GcsAllowEmptyValues,
   108  				GcsAdditionalFields,
   109  			))
   110  			resources = append(resources, g.createNotificationResources(gcsService, bucket)...)
   111  		}
   112  		return nil
   113  	}); err != nil {
   114  		log.Println(err)
   115  	}
   116  	return resources
   117  }
   118  
   119  func (g *GcsGenerator) createNotificationResources(gcsService *storage.Service, bucket *storage.Bucket) []terraformutils.Resource {
   120  	resources := []terraformutils.Resource{}
   121  	notificationList, err := gcsService.Notifications.List(bucket.Name).Do()
   122  	if err != nil {
   123  		log.Println(err)
   124  		return resources
   125  	}
   126  	for _, notification := range notificationList.Items {
   127  		resources = append(resources, terraformutils.NewResource(
   128  			bucket.Name+"/notificationConfigs/"+notification.Id,
   129  			bucket.Name+"/"+notification.Id,
   130  			"google_storage_notification",
   131  			g.ProviderName,
   132  			map[string]string{},
   133  			GcsAllowEmptyValues,
   134  			GcsAdditionalFields,
   135  		))
   136  	}
   137  	return resources
   138  }
   139  
   140  /*
   141  func (g *GcsGenerator) createTransferJobsResources(ctx context.Context, storageTransferService *storagetransfer.Service) []terraformutils.Resource {
   142  	resources := []terraformutils.Resource{}
   143  	transferJobsList := storageTransferService.TransferJobs.List()
   144  	err := transferJobsList.Pages(ctx, func(page *storagetransfer.ListTransferJobsResponse) error {
   145  		log.Println(page.TransferJobs)
   146  		for _, transferJob := range page.TransferJobs {
   147  			resources = append(resources, terraformutils.NewResource(
   148  				transferJob.Name,
   149  				transferJob.Name,
   150  				"google_storage_transfer_job",
   151  				g.ProviderName,
   152  				map[string]string{
   153  					"name": transferJob.Name,
   154  				},
   155  				GcsAllowEmptyValues,
   156  				GcsAdditionalFields,
   157  			))
   158  		}
   159  		return nil
   160  	})
   161  	if err != nil {
   162  		log.Fatal(err)
   163  	}
   164  	return resources
   165  }
   166  */
   167  
   168  // Generate TerraformResources from GCP API,
   169  // from each bucket  create 1 TerraformResource
   170  // Need bucket name as ID for terraform resource
   171  func (g *GcsGenerator) InitResources() error {
   172  	ctx := context.Background()
   173  	gcsService, err := storage.NewService(ctx)
   174  	if err != nil {
   175  		log.Print(err)
   176  		return err
   177  	}
   178  	g.Resources = g.createBucketsResources(ctx, gcsService)
   179  
   180  	// TODO find bug with storageTransferService.TransferJobs.List().Pages
   181  	// storageTransferService, err := storagetransfer.NewService(ctx)
   182  	// if err != nil {
   183  	// 	log.Print(err)
   184  	// 		return err
   185  	// 	}
   186  	// g.Resources = append(g.Resources, g.createTransferJobsResources(ctx, storageTransferService)...)
   187  	return nil
   188  }
   189  
   190  // PostGenerateHook for add bucket policy json as heredoc
   191  // support only bucket with policy
   192  func (g *GcsGenerator) PostConvertHook() error {
   193  	for i, resource := range g.Resources {
   194  		if resource.InstanceInfo.Type != "google_storage_bucket_iam_policy" {
   195  			continue
   196  		}
   197  		if _, exist := resource.Item["policy_data"]; exist {
   198  			policy := resource.Item["policy_data"].(string)
   199  			g.Resources[i].Item["policy_data"] = fmt.Sprintf(`<<POLICY
   200  %s
   201  POLICY`, policy)
   202  		}
   203  	}
   204  	return nil
   205  }