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

     1  // Copyright 2019 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  	"context"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  	"github.com/okta/okta-sdk-golang/v2/okta"
    24  )
    25  
    26  type UserSchemaPropertyGenerator struct {
    27  	OktaService
    28  }
    29  
    30  func (g UserSchemaPropertyGenerator) createResources(userSchema *okta.UserSchema, userTypeID string, userTypeName string) []terraformutils.Resource {
    31  	var resources []terraformutils.Resource
    32  	for index := range userSchema.Definitions.Custom.Properties {
    33  		resources = append(resources, terraformutils.NewResource(
    34  			index,
    35  			normalizeResourceName(userTypeName)+"_property_"+normalizeResourceName(index),
    36  			"okta_user_schema_property",
    37  			"okta",
    38  			map[string]string{
    39  				"index":     index,
    40  				"user_type": userTypeID,
    41  			},
    42  			[]string{},
    43  			map[string]interface{}{},
    44  		))
    45  	}
    46  
    47  	for index := range userSchema.Definitions.Base.Properties {
    48  		resources = append(resources, terraformutils.NewResource(
    49  			index,
    50  			normalizeResourceName(userTypeName)+"_property_"+normalizeResourceName(index),
    51  			"okta_user_base_schema_property",
    52  			"okta",
    53  			map[string]string{
    54  				"index":     index,
    55  				"user_type": userTypeID,
    56  			},
    57  			[]string{},
    58  			map[string]interface{}{},
    59  		))
    60  	}
    61  	return resources
    62  }
    63  
    64  func (g *UserSchemaPropertyGenerator) InitResources() error {
    65  	var resources []terraformutils.Resource
    66  	ctx, client, e := g.Client()
    67  	if e != nil {
    68  		return e
    69  	}
    70  
    71  	userTypes, err := getUserTypes(ctx, client)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	for _, userType := range userTypes {
    77  		schemaID := getUserTypeSchemaID(userType)
    78  		if schemaID != "" {
    79  			schema, _, err := client.UserSchema.GetUserSchema(ctx, schemaID)
    80  			if err != nil {
    81  				return err
    82  			}
    83  
    84  			userTypeID := "default"
    85  			if userType.Name != "user" {
    86  				userTypeID = userType.Id
    87  			}
    88  
    89  			resources = append(resources, g.createResources(schema, userTypeID, userType.Name)...)
    90  		}
    91  	}
    92  
    93  	g.Resources = resources
    94  	return nil
    95  }
    96  
    97  func getUserTypes(ctx context.Context, client *okta.Client) ([]*okta.UserType, error) {
    98  	output, resp, err := client.UserType.ListUserTypes(ctx)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	for resp.HasNextPage() {
   104  		var nextUserTypeSet []*okta.UserType
   105  		resp, _ = resp.Next(ctx, &nextUserTypeSet)
   106  		output = append(output, nextUserTypeSet...)
   107  	}
   108  
   109  	return output, nil
   110  }
   111  
   112  func getUserTypeSchemaID(ut *okta.UserType) string {
   113  	fm, ok := ut.Links.(map[string]interface{})
   114  	if ok {
   115  		sm, ok := fm["schema"].(map[string]interface{})
   116  		if ok {
   117  			href, ok := sm["href"].(string)
   118  			if ok {
   119  				u, _ := url.Parse(href)
   120  				return strings.TrimPrefix(u.EscapedPath(), "/api/v1/meta/schemas/user/")
   121  			}
   122  		}
   123  	}
   124  	return ""
   125  }