github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/kms.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  	"log"
    20  	"strings"
    21  
    22  	"google.golang.org/api/cloudkms/v1"
    23  
    24  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    25  )
    26  
    27  var kmsAllowEmptyValues = []string{""}
    28  
    29  var kmsAdditionalFields = map[string]interface{}{}
    30  
    31  type KmsGenerator struct {
    32  	GCPService
    33  }
    34  
    35  func (g KmsGenerator) createKmsRingResources(ctx context.Context, keyRingList *cloudkms.ProjectsLocationsKeyRingsListCall, kmsService *cloudkms.Service) []terraformutils.Resource {
    36  	resources := []terraformutils.Resource{}
    37  	if err := keyRingList.Pages(ctx, func(page *cloudkms.ListKeyRingsResponse) error {
    38  		for _, obj := range page.KeyRings {
    39  			tm := strings.Split(obj.Name, "/")
    40  			ID := tm[1] + "/" + tm[3] + "/" + tm[5]
    41  			resources = append(resources, terraformutils.NewResource(
    42  				ID,
    43  				tm[len(tm)-3]+"_"+tm[len(tm)-1],
    44  				"google_kms_key_ring",
    45  				g.ProviderName,
    46  				map[string]string{
    47  					"project":  g.GetArgs()["project"].(string),
    48  					"location": tm[3],
    49  					"name":     tm[5],
    50  				},
    51  				kmsAllowEmptyValues,
    52  				kmsAdditionalFields,
    53  			))
    54  			resources = append(resources, g.createKmsKeyResources(ctx, obj.Name, kmsService)...)
    55  		}
    56  		return nil
    57  	}); err != nil {
    58  		log.Println(err)
    59  	}
    60  	return resources
    61  }
    62  
    63  func (g *KmsGenerator) createKmsKeyResources(ctx context.Context, keyRingName string, kmsService *cloudkms.Service) []terraformutils.Resource {
    64  	resources := []terraformutils.Resource{}
    65  	keyList := kmsService.Projects.Locations.KeyRings.CryptoKeys.List(keyRingName)
    66  	if err := keyList.Pages(ctx, func(page *cloudkms.ListCryptoKeysResponse) error {
    67  		for _, key := range page.CryptoKeys {
    68  			tm := strings.Split(key.Name, "/")
    69  			resources = append(resources, terraformutils.NewResource(
    70  				key.Name,
    71  				tm[1]+"_"+tm[3]+"_"+tm[5]+"_"+tm[7],
    72  				"google_kms_crypto_key",
    73  				g.ProviderName,
    74  				map[string]string{
    75  					"project": g.GetArgs()["project"].(string),
    76  					"name":    key.Name,
    77  				},
    78  				kmsAllowEmptyValues,
    79  				kmsAdditionalFields,
    80  			))
    81  		}
    82  		return nil
    83  	}); err != nil {
    84  		log.Println(err)
    85  	}
    86  	return resources
    87  }
    88  
    89  // Generate TerraformResources from GCP API,
    90  func (g *KmsGenerator) InitResources() error {
    91  	ctx := context.Background()
    92  	kmsService, err := cloudkms.NewService(ctx)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	keyRingList := kmsService.Projects.Locations.KeyRings.List("projects/" + g.GetArgs()["project"].(string) + "/locations/global")
    98  
    99  	g.Resources = g.createKmsRingResources(ctx, keyRingList, kmsService)
   100  	return nil
   101  }
   102  
   103  func (g *KmsGenerator) PostConvertHook() error {
   104  	for i, key := range g.Resources {
   105  		if key.InstanceInfo.Type != "google_kms_crypto_key" {
   106  			continue
   107  		}
   108  		for _, keyRing := range g.Resources {
   109  			if keyRing.InstanceInfo.Type != "google_kms_key_ring" {
   110  				continue
   111  			}
   112  			if key.Item["key_ring"] == keyRing.InstanceState.ID {
   113  				g.Resources[i].Item["key_ring"] = "${google_kms_key_ring." + keyRing.ResourceName + ".self_link}"
   114  			}
   115  		}
   116  	}
   117  	return nil
   118  }