github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/cloudflare/account_member.go (about)

     1  // Copyright 2020 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 cloudflare
    16  
    17  import (
    18  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    19  	cf "github.com/cloudflare/cloudflare-go"
    20  )
    21  
    22  type AccountMemberGenerator struct {
    23  	CloudflareService
    24  }
    25  
    26  func (g *AccountMemberGenerator) createAccountMemberResources(api *cf.API) ([]terraformutils.Resource, error) {
    27  	var resources []terraformutils.Resource
    28  	pageOpt := cf.PaginationOptions{
    29  		Page:    1,
    30  		PerPage: 10}
    31  
    32  	for {
    33  		members, info, err := api.AccountMembers(api.AccountID, pageOpt)
    34  		if err != nil {
    35  			return resources, err
    36  		}
    37  
    38  		for _, member := range members {
    39  			var roleIDs []string
    40  			for _, role := range member.Roles {
    41  				roleIDs = append(roleIDs, role.ID)
    42  			}
    43  
    44  			resources = append(resources, terraformutils.NewResource(
    45  				member.ID,
    46  				member.ID,
    47  				"cloudflare_account_member",
    48  				"cloudflare",
    49  				map[string]string{
    50  					"email_address": member.User.Email,
    51  				},
    52  				[]string{},
    53  				map[string]interface{}{
    54  					"role_ids": roleIDs,
    55  				},
    56  			))
    57  		}
    58  
    59  		if pageOpt.Page < info.TotalPages {
    60  			pageOpt.Page++
    61  		} else {
    62  			break
    63  		}
    64  	}
    65  
    66  	return resources, nil
    67  }
    68  
    69  func (g *AccountMemberGenerator) InitResources() error {
    70  	api, err := g.initializeAPI()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	resources, err := g.createAccountMemberResources(api)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	g.Resources = append(g.Resources, resources...)
    79  
    80  	return nil
    81  }