github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/datadog/user.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 datadog 16 17 import ( 18 "context" 19 "fmt" 20 "strings" 21 22 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 23 24 datadogV2 "github.com/DataDog/datadog-api-client-go/api/v2/datadog" 25 ) 26 27 var ( 28 // UserAllowEmptyValues ... 29 UserAllowEmptyValues = []string{} 30 ) 31 32 // UserGenerator ... 33 type UserGenerator struct { 34 DatadogService 35 } 36 37 func (g *UserGenerator) createResources(users []datadogV2.User) []terraformutils.Resource { 38 resources := []terraformutils.Resource{} 39 for _, user := range users { 40 relations := user.GetRelationships() 41 roles := relations.GetRoles() 42 // If no roles are present, we can assume user was created via the V1 API 43 // Hence, import the user via their handle 44 if len(roles.GetData()) == 0 { 45 attr := user.GetAttributes() 46 resources = append(resources, g.createResource(attr.GetHandle())) 47 continue 48 } 49 50 resources = append(resources, g.createResource(user.GetId())) 51 } 52 return resources 53 } 54 55 func (g *UserGenerator) createResource(userID string) terraformutils.Resource { 56 return terraformutils.NewSimpleResource( 57 userID, 58 fmt.Sprintf("user_%s", userID), 59 "datadog_user", 60 "datadog", 61 UserAllowEmptyValues, 62 ) 63 } 64 65 // InitResources Generate TerraformResources from Datadog API, 66 // from each user create 1 TerraformResource. 67 // Need User ID as ID for terraform resource 68 func (g *UserGenerator) InitResources() error { 69 var users []datadogV2.User 70 datadogClientV2 := g.Args["datadogClientV2"].(*datadogV2.APIClient) 71 authV2 := g.Args["authV2"].(context.Context) 72 73 pageSize := int64(1000) 74 pageNumber := int64(0) 75 remaining := int64(1) 76 optionalParams := datadogV2.NewListUsersOptionalParameters() 77 for _, filter := range g.Filter { 78 if filter.IsApplicable("user") && filter.FieldPath == "disabled" { 79 if len(filter.AcceptableValues) == 1 && strings.ToLower(filter.AcceptableValues[0]) == "false" { 80 optionalParams = optionalParams.WithFilterStatus("Active,Pending") 81 } 82 } 83 } 84 85 for remaining > int64(0) { 86 resp, _, err := datadogClientV2.UsersApi.ListUsers(authV2, *optionalParams. 87 WithPageSize(pageSize). 88 WithPageNumber(pageNumber)) 89 if err != nil { 90 return err 91 } 92 users = append(users, resp.GetData()...) 93 94 remaining = resp.Meta.Page.GetTotalCount() - pageSize*(pageNumber+1) 95 pageNumber++ 96 } 97 98 g.Resources = g.createResources(users) 99 return nil 100 }