github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/okta/policy_password.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  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/okta/okta-sdk-golang/v2/okta"
    22  	"github.com/okta/okta-sdk-golang/v2/okta/query"
    23  )
    24  
    25  type PasswordPolicyGenerator struct {
    26  	OktaService
    27  }
    28  
    29  func (g PasswordPolicyGenerator) createResources(passwordPolicyList []*okta.Policy) []terraformutils.Resource {
    30  	var resources []terraformutils.Resource
    31  	for _, passwordPolicy := range passwordPolicyList {
    32  		resourceName := normalizeResourceName(passwordPolicy.Name)
    33  		resourceType := "okta_policy_password"
    34  		if passwordPolicy.Name == "Default Policy" {
    35  			resourceType = "okta_policy_password_default"
    36  		}
    37  		resources = append(resources, terraformutils.NewSimpleResource(
    38  			passwordPolicy.Id,
    39  			"policy_password_"+resourceName,
    40  			resourceType,
    41  			"okta",
    42  			[]string{}))
    43  	}
    44  	return resources
    45  }
    46  
    47  func (g *PasswordPolicyGenerator) InitResources() error {
    48  	var output []*okta.Policy
    49  	ctx, client, e := g.Client()
    50  	if e != nil {
    51  		return e
    52  	}
    53  
    54  	output, _ = getPasswordPolicies(ctx, client)
    55  	g.Resources = g.createResources(output)
    56  	return nil
    57  }
    58  
    59  func getPasswordPolicies(ctx context.Context, client *okta.Client) ([]*okta.Policy, error) {
    60  	qp := query.NewQueryParams(query.WithType("PASSWORD"))
    61  	output, resp, err := client.Policy.ListPolicies(ctx, qp)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	for resp.HasNextPage() {
    67  		var nextPolicySet []*okta.Policy
    68  		resp, _ = resp.Next(ctx, &nextPolicySet)
    69  		output = append(output, nextPolicySet...)
    70  	}
    71  
    72  	return output, nil
    73  }