github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/panos/panorama_plugins.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 panos
    16  
    17  import (
    18  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    19  	"github.com/PaloAltoNetworks/pango"
    20  )
    21  
    22  type PanoramaPluginsGenerator struct {
    23  	PanosService
    24  }
    25  
    26  func (g *PanoramaPluginsGenerator) createGCPAccountResources() (resources []terraformutils.Resource) {
    27  	l, err := g.client.(*pango.Panorama).Panorama.GcpAccount.GetList()
    28  	if err != nil || len(l) == 0 {
    29  		return resources
    30  	}
    31  
    32  	for _, r := range l {
    33  		resources = append(resources, terraformutils.NewSimpleResource(
    34  			r,
    35  			normalizeResourceName(r),
    36  			"panos_panorama_gcp_account",
    37  			"panos",
    38  			[]string{},
    39  		))
    40  	}
    41  
    42  	return resources
    43  }
    44  
    45  func (g *PanoramaPluginsGenerator) createGKEClusterResources(group string) (resources []terraformutils.Resource) {
    46  	l, err := g.client.(*pango.Panorama).Panorama.GkeCluster.GetList(group)
    47  	if err != nil || len(l) == 0 {
    48  		return resources
    49  	}
    50  
    51  	for _, r := range l {
    52  		id := group + ":" + r
    53  		resources = append(resources, terraformutils.NewSimpleResource(
    54  			id,
    55  			normalizeResourceName(id),
    56  			"panos_panorama_gke_cluster",
    57  			"panos",
    58  			[]string{},
    59  		))
    60  	}
    61  
    62  	return resources
    63  }
    64  
    65  func (g *PanoramaPluginsGenerator) createGKEClusterGroupResources() (resources []terraformutils.Resource) {
    66  	l, err := g.client.(*pango.Panorama).Panorama.GkeClusterGroup.GetList()
    67  	if err != nil || len(l) == 0 {
    68  		return resources
    69  	}
    70  
    71  	for _, r := range l {
    72  		resources = append(resources, terraformutils.NewSimpleResource(
    73  			r,
    74  			normalizeResourceName(r),
    75  			"panos_panorama_gke_cluster_group",
    76  			"panos",
    77  			[]string{},
    78  		))
    79  
    80  		resources = append(resources, g.createGKEClusterResources(r)...)
    81  	}
    82  
    83  	return resources
    84  }
    85  
    86  func (g *PanoramaPluginsGenerator) InitResources() error {
    87  	if err := g.Initialize(); err != nil {
    88  		return err
    89  	}
    90  
    91  	g.Resources = append(g.Resources, g.createGCPAccountResources()...)
    92  	g.Resources = append(g.Resources, g.createGKEClusterGroupResources()...)
    93  
    94  	return nil
    95  }
    96  
    97  func (g *PanoramaPluginsGenerator) PostConvertHook() error {
    98  	mapGKEClusterGroupNames := map[string]string{}
    99  
   100  	for _, r := range g.Resources {
   101  		if r.InstanceInfo.Type == "panos_panorama_gke_cluster_group" {
   102  			mapGKEClusterGroupNames[r.Item["name"].(string)] = "${" + r.InstanceInfo.Type + "." + r.ResourceName + ".name}"
   103  		}
   104  	}
   105  
   106  	for _, r := range g.Resources {
   107  		if r.InstanceInfo.Type == "panos_panorama_gke_cluster" {
   108  			r.Item["gke_cluster_group"] = mapGKEClusterGroupNames[r.Item["gke_cluster_group"].(string)]
   109  		}
   110  	}
   111  
   112  	return nil
   113  }