github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/panos/panorama_objects.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 PanoramaObjectsGenerator struct {
    23  	PanosService
    24  }
    25  
    26  func (g *PanoramaObjectsGenerator) createResourcesFromList(o getGeneric, dg string, terraformResourceName string) (resources []terraformutils.Resource) {
    27  	l, err := o.i.(getListWithOneArg).GetList(o.params[0])
    28  	if err != nil || len(l) == 0 {
    29  		return []terraformutils.Resource{}
    30  	}
    31  
    32  	for _, r := range l {
    33  		id := dg + ":" + r
    34  		resources = append(resources, terraformutils.NewResource(
    35  			id,
    36  			normalizeResourceName(id),
    37  			terraformResourceName,
    38  			"panos",
    39  			map[string]string{
    40  				"device_group": dg,
    41  			},
    42  			[]string{},
    43  			map[string]interface{}{},
    44  		))
    45  	}
    46  
    47  	return resources
    48  }
    49  
    50  func (g *PanoramaObjectsGenerator) createAddressGroupResources(dg string) []terraformutils.Resource {
    51  	return g.createResourcesFromList(
    52  		getGeneric{g.client.(*pango.Panorama).Objects.AddressGroup, []string{dg}},
    53  		dg, "panos_panorama_address_group",
    54  	)
    55  }
    56  
    57  func (g *PanoramaObjectsGenerator) createAdministrativeTagResources(dg string) []terraformutils.Resource {
    58  	return g.createResourcesFromList(
    59  		getGeneric{g.client.(*pango.Panorama).Objects.Tags, []string{dg}},
    60  		dg, "panos_panorama_administrative_tag",
    61  	)
    62  }
    63  
    64  func (g *PanoramaObjectsGenerator) createApplicationGroupResources(dg string) []terraformutils.Resource {
    65  	return g.createResourcesFromList(
    66  		getGeneric{g.client.(*pango.Panorama).Objects.AppGroup, []string{dg}},
    67  		dg, "panos_panorama_application_group",
    68  	)
    69  }
    70  
    71  func (g *PanoramaObjectsGenerator) createApplicationObjectResources(dg string) (resources []terraformutils.Resource) {
    72  	l, err := g.client.(*pango.Panorama).Objects.Application.GetList(dg)
    73  	if err != nil {
    74  		return []terraformutils.Resource{}
    75  	}
    76  
    77  	for _, r := range l {
    78  		id := dg + ":" + r
    79  		resources = append(resources, terraformutils.NewSimpleResource(
    80  			id,
    81  			normalizeResourceName(id),
    82  			"panos_panorama_application_object",
    83  			"panos",
    84  			[]string{},
    85  		))
    86  
    87  		// TODO
    88  		// resources = append(resources, g.createApplicationSignatureResources(dg, r)...)
    89  	}
    90  
    91  	return resources
    92  }
    93  
    94  func (g *PanoramaObjectsGenerator) createEDLResources(dg string) []terraformutils.Resource {
    95  	return g.createResourcesFromList(
    96  		getGeneric{g.client.(*pango.Panorama).Objects.Edl, []string{dg}},
    97  		dg, "panos_panorama_edl",
    98  	)
    99  }
   100  
   101  func (g *PanoramaObjectsGenerator) createLogForwardingResources(dg string) []terraformutils.Resource {
   102  	return g.createResourcesFromList(
   103  		getGeneric{g.client.(*pango.Panorama).Objects.LogForwardingProfile, []string{dg}},
   104  		dg, "panos_panorama_log_forwarding_profile",
   105  	)
   106  }
   107  
   108  func (g *PanoramaObjectsGenerator) createServiceGroupResources(dg string) []terraformutils.Resource {
   109  	return g.createResourcesFromList(
   110  		getGeneric{g.client.(*pango.Panorama).Objects.ServiceGroup, []string{dg}},
   111  		dg, "panos_panorama_service_group",
   112  	)
   113  }
   114  
   115  func (g *PanoramaObjectsGenerator) createServiceObjectResources(dg string) []terraformutils.Resource {
   116  	return g.createResourcesFromList(
   117  		getGeneric{g.client.(*pango.Panorama).Objects.Services, []string{dg}},
   118  		dg, "panos_panorama_service_object",
   119  	)
   120  }
   121  
   122  func (g *PanoramaObjectsGenerator) createAddressObjectResources(dg string) []terraformutils.Resource {
   123  	return g.createResourcesFromList(
   124  		getGeneric{g.client.(*pango.Panorama).Objects.Address, []string{dg}},
   125  		dg, "panos_address_object",
   126  	)
   127  }
   128  
   129  func (g *PanoramaObjectsGenerator) createAntiSpywareSecurityProfileResources(dg string) []terraformutils.Resource {
   130  	return g.createResourcesFromList(
   131  		getGeneric{g.client.(*pango.Panorama).Objects.AntiSpywareProfile, []string{dg}},
   132  		dg, "panos_anti_spyware_security_profile",
   133  	)
   134  }
   135  
   136  func (g *PanoramaObjectsGenerator) createAntivirusSecurityProfileResources(dg string) []terraformutils.Resource {
   137  	return g.createResourcesFromList(
   138  		getGeneric{g.client.(*pango.Panorama).Objects.AntivirusProfile, []string{dg}},
   139  		dg, "panos_antivirus_security_profile",
   140  	)
   141  }
   142  
   143  func (g *PanoramaObjectsGenerator) createCustomDataPatternObjectResources(dg string) []terraformutils.Resource {
   144  	return g.createResourcesFromList(
   145  		getGeneric{g.client.(*pango.Panorama).Objects.DataPattern, []string{dg}},
   146  		dg, "panos_custom_data_pattern_object",
   147  	)
   148  }
   149  
   150  func (g *PanoramaObjectsGenerator) createDataFilteringSecurityProfileResources(dg string) []terraformutils.Resource {
   151  	return g.createResourcesFromList(
   152  		getGeneric{g.client.(*pango.Panorama).Objects.DataFilteringProfile, []string{dg}},
   153  		dg, "panos_data_filtering_security_profile",
   154  	)
   155  }
   156  
   157  func (g *PanoramaObjectsGenerator) createDOSProtectionProfileResources(dg string) []terraformutils.Resource {
   158  	return g.createResourcesFromList(
   159  		getGeneric{g.client.(*pango.Panorama).Objects.DosProtectionProfile, []string{dg}},
   160  		dg, "panos_dos_protection_profile",
   161  	)
   162  }
   163  
   164  func (g *PanoramaObjectsGenerator) createDynamicUserGroupResources(dg string) []terraformutils.Resource {
   165  	return g.createResourcesFromList(
   166  		getGeneric{g.client.(*pango.Panorama).Objects.DynamicUserGroup, []string{dg}},
   167  		dg, "panos_dynamic_user_group",
   168  	)
   169  }
   170  
   171  func (g *PanoramaObjectsGenerator) createFileBlockingSecurityProfileResources(dg string) []terraformutils.Resource {
   172  	return g.createResourcesFromList(
   173  		getGeneric{g.client.(*pango.Panorama).Objects.FileBlockingProfile, []string{dg}},
   174  		dg, "panos_file_blocking_security_profile",
   175  	)
   176  }
   177  
   178  func (g *PanoramaObjectsGenerator) createURLFilteringSecurityProfileResources(dg string) []terraformutils.Resource {
   179  	return g.createResourcesFromList(
   180  		getGeneric{g.client.(*pango.Panorama).Objects.UrlFilteringProfile, []string{dg}},
   181  		dg, "panos_url_filtering_security_profile",
   182  	)
   183  }
   184  
   185  func (g *PanoramaObjectsGenerator) createVulnerabilitySecurityProfileResources(dg string) []terraformutils.Resource {
   186  	return g.createResourcesFromList(
   187  		getGeneric{g.client.(*pango.Panorama).Objects.VulnerabilityProfile, []string{dg}},
   188  		dg, "panos_vulnerability_security_profile",
   189  	)
   190  }
   191  
   192  func (g *PanoramaObjectsGenerator) createWildfireAnalysisSecurityProfileResources(dg string) []terraformutils.Resource {
   193  	return g.createResourcesFromList(
   194  		getGeneric{g.client.(*pango.Panorama).Objects.WildfireAnalysisProfile, []string{dg}},
   195  		dg, "panos_wildfire_analysis_security_profile",
   196  	)
   197  }
   198  func (g *PanoramaObjectsGenerator) InitResources() error {
   199  	if err := g.Initialize(); err != nil {
   200  		return err
   201  	}
   202  
   203  	dg, err := g.client.(*pango.Panorama).Panorama.DeviceGroup.GetList()
   204  	if err != nil {
   205  		return err
   206  	}
   207  
   208  	for _, v := range dg {
   209  		g.Resources = append(g.Resources, g.createAddressGroupResources(v)...)
   210  		g.Resources = append(g.Resources, g.createAdministrativeTagResources(v)...)
   211  		g.Resources = append(g.Resources, g.createApplicationGroupResources(v)...)
   212  		g.Resources = append(g.Resources, g.createApplicationObjectResources(v)...)
   213  		g.Resources = append(g.Resources, g.createEDLResources(v)...)
   214  		g.Resources = append(g.Resources, g.createLogForwardingResources(v)...)
   215  		g.Resources = append(g.Resources, g.createServiceGroupResources(v)...)
   216  		g.Resources = append(g.Resources, g.createServiceObjectResources(v)...)
   217  		g.Resources = append(g.Resources, g.createAddressObjectResources(v)...)
   218  		g.Resources = append(g.Resources, g.createAntiSpywareSecurityProfileResources(v)...)
   219  		g.Resources = append(g.Resources, g.createAntivirusSecurityProfileResources(v)...)
   220  		g.Resources = append(g.Resources, g.createCustomDataPatternObjectResources(v)...)
   221  		g.Resources = append(g.Resources, g.createDataFilteringSecurityProfileResources(v)...)
   222  		g.Resources = append(g.Resources, g.createDOSProtectionProfileResources(v)...)
   223  		g.Resources = append(g.Resources, g.createDynamicUserGroupResources(v)...)
   224  		g.Resources = append(g.Resources, g.createFileBlockingSecurityProfileResources(v)...)
   225  		g.Resources = append(g.Resources, g.createURLFilteringSecurityProfileResources(v)...)
   226  		g.Resources = append(g.Resources, g.createVulnerabilitySecurityProfileResources(v)...)
   227  		g.Resources = append(g.Resources, g.createWildfireAnalysisSecurityProfileResources(v)...)
   228  	}
   229  
   230  	return nil
   231  }
   232  
   233  func (g *PanoramaObjectsGenerator) PostConvertHook() error {
   234  	mapAddressObjectIDs := map[string]string{}
   235  	mapApplicationObjectIDs := map[string]string{}
   236  	mapServiceObjectIDs := map[string]string{}
   237  
   238  	for _, r := range g.Resources {
   239  		if r.InstanceInfo.Type == "panos_address_object" {
   240  			mapAddressObjectIDs[r.Item["name"].(string)] = "${" + r.InstanceInfo.Type + "." + r.ResourceName + ".name}"
   241  		}
   242  
   243  		if r.InstanceInfo.Type == "panos_panorama_application_object" {
   244  			mapApplicationObjectIDs[r.Item["name"].(string)] = "${" + r.InstanceInfo.Type + "." + r.ResourceName + ".name}"
   245  		}
   246  
   247  		if r.InstanceInfo.Type == "panos_panorama_service_object" {
   248  			mapServiceObjectIDs[r.Item["name"].(string)] = "${" + r.InstanceInfo.Type + "." + r.ResourceName + ".name}"
   249  		}
   250  	}
   251  
   252  	for _, r := range g.Resources {
   253  		if r.InstanceInfo.Type == "panos_panorama_address_group" {
   254  			if _, ok := r.Item["static_addresses"]; ok {
   255  				staticAddresses := make([]string, len(r.Item["static_addresses"].([]interface{})))
   256  				for k, staticAddress := range r.Item["static_addresses"].([]interface{}) {
   257  					staticAddresses[k] = mapAddressObjectIDs[staticAddress.(string)]
   258  				}
   259  
   260  				r.Item["static_addresses"] = staticAddresses
   261  			}
   262  		}
   263  
   264  		if r.InstanceInfo.Type == "panos_panorama_application_group" {
   265  			if _, ok := r.Item["applications"]; ok {
   266  				applications := make([]string, len(r.Item["applications"].([]interface{})))
   267  				for k, application := range r.Item["applications"].([]interface{}) {
   268  					if _, ok := mapApplicationObjectIDs[application.(string)]; ok {
   269  						applications[k] = mapApplicationObjectIDs[application.(string)]
   270  						continue
   271  					}
   272  					applications[k] = application.(string)
   273  				}
   274  
   275  				r.Item["applications"] = applications
   276  			}
   277  		}
   278  
   279  		if r.InstanceInfo.Type == "panos_panorama_service_group" {
   280  			services := make([]string, len(r.Item["services"].([]interface{})))
   281  			for k, service := range r.Item["services"].([]interface{}) {
   282  				services[k] = mapServiceObjectIDs[service.(string)]
   283  			}
   284  
   285  			r.Item["services"] = services
   286  		}
   287  	}
   288  
   289  	return nil
   290  }