github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/okta/policy_rule_mfa.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 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 19 "github.com/okta/terraform-provider-okta/sdk" 20 ) 21 22 type MFAPolicyRuleGenerator struct { 23 OktaService 24 } 25 26 func (g MFAPolicyRuleGenerator) createResources(mfaPolicyRuleList []sdk.PolicyRule, policyID string, policyName string) []terraformutils.Resource { 27 var resources []terraformutils.Resource 28 29 for _, policyRule := range mfaPolicyRuleList { 30 resources = append(resources, terraformutils.NewResource( 31 policyRule.Id, 32 "policyrule_mfa_"+normalizeResourceName(policyName+"_"+policyRule.Name), 33 "okta_policy_rule_mfa", 34 "okta", 35 map[string]string{ 36 "policy_id": policyID, 37 }, 38 []string{}, 39 map[string]interface{}{}, 40 )) 41 } 42 43 return resources 44 } 45 46 func (g *MFAPolicyRuleGenerator) InitResources() error { 47 var resources []terraformutils.Resource 48 49 ctx, client, e := g.Client() 50 if e != nil { 51 return e 52 } 53 54 mfaPolicies, err := getMFAPolicies(ctx, client) 55 if err != nil { 56 return err 57 } 58 59 for _, policy := range mfaPolicies { 60 output, err := getMFAPolicyRules(g, policy.Id) 61 if err != nil { 62 return err 63 } 64 65 resources = append(resources, g.createResources(output, policy.Id, policy.Name)...) 66 } 67 68 g.Resources = resources 69 return nil 70 } 71 72 func getMFAPolicyRules(g *MFAPolicyRuleGenerator, policyID string) ([]sdk.PolicyRule, error) { 73 ctx, client, e := g.APISupplementClient() 74 if e != nil { 75 return nil, e 76 } 77 78 output, resp, err := client.ListPolicyRules(ctx, policyID) 79 if err != nil { 80 return nil, e 81 } 82 83 for resp.HasNextPage() { 84 var nextPolicySet []sdk.PolicyRule 85 resp, _ = resp.Next(ctx, &nextPolicySet) 86 output = append(output, nextPolicySet...) 87 } 88 89 return output, nil 90 }