github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/okta/network_zone.go (about)

     1  // Copyright 2021 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 okta
    16  
    17  import (
    18  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    19  	"github.com/okta/okta-sdk-golang/v2/okta"
    20  )
    21  
    22  type NetworkZoneGenerator struct {
    23  	OktaService
    24  }
    25  
    26  func (g NetworkZoneGenerator) createResources(networkZoneList []*okta.NetworkZone) []terraformutils.Resource {
    27  	var resources []terraformutils.Resource
    28  	for _, networkZone := range networkZoneList {
    29  
    30  		resources = append(resources, terraformutils.NewResource(
    31  			networkZone.Id,
    32  			networkZone.Name,
    33  			"okta_network_zone",
    34  			"okta",
    35  			map[string]string{
    36  				"name": networkZone.Name,
    37  				"type": networkZone.Type,
    38  			},
    39  			[]string{},
    40  			attributesNetworkZone(networkZone),
    41  		))
    42  	}
    43  	return resources
    44  }
    45  
    46  func (g *NetworkZoneGenerator) InitResources() error {
    47  	ctx, client, err := g.Client()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	output, resp, err := client.NetworkZone.ListNetworkZones(ctx, nil)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	for resp.HasNextPage() {
    58  		var networkZoneSet []*okta.NetworkZone
    59  		resp, _ = resp.Next(ctx, &networkZoneSet)
    60  		output = append(output, networkZoneSet...)
    61  	}
    62  
    63  	g.Resources = g.createResources(output)
    64  	return nil
    65  }
    66  
    67  func attributesNetworkZone(networkZone *okta.NetworkZone) map[string]interface{} {
    68  	attributes := map[string]interface{}{}
    69  	attributes["usage"] = networkZone.Usage
    70  
    71  	if networkZone.Type == "DYNAMIC" {
    72  		if networkZone.Locations != nil {
    73  			attributes["dynamic_locations"] = networkZone.Locations
    74  		}
    75  	} else if networkZone.Type == "IP" {
    76  		switch {
    77  		case networkZone.Proxies != nil && networkZone.Gateways != nil:
    78  			attributes["proxies"] = networkZone.Proxies
    79  			attributes["gateways"] = networkZone.Gateways
    80  		case networkZone.Proxies != nil && networkZone.Gateways == nil:
    81  			attributes["proxies"] = networkZone.Proxies
    82  		case networkZone.Proxies == nil && networkZone.Gateways != nil:
    83  			attributes["gateways"] = networkZone.Gateways
    84  		}
    85  	}
    86  
    87  	return attributes
    88  }