github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/pagerduty/escalation_policy.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 pagerduty
    16  
    17  import (
    18  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    19  	pagerduty "github.com/heimweh/go-pagerduty/pagerduty"
    20  )
    21  
    22  type EscalationPolicyGenerator struct {
    23  	PagerDutyService
    24  }
    25  
    26  func (g *EscalationPolicyGenerator) createEscalationPolicyResources(client *pagerduty.Client) error {
    27  	var offset = 0
    28  	options := pagerduty.ListEscalationPoliciesOptions{}
    29  	for {
    30  		options.Offset = offset
    31  		resp, _, err := client.EscalationPolicies.List(&options)
    32  		if err != nil {
    33  			return err
    34  		}
    35  
    36  		for _, policy := range resp.EscalationPolicies {
    37  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    38  				policy.ID,
    39  				policy.Name,
    40  				"pagerduty_escalation_policy",
    41  				g.ProviderName,
    42  				[]string{},
    43  			))
    44  		}
    45  
    46  		if !resp.More {
    47  			break
    48  		}
    49  
    50  		offset += resp.Limit
    51  	}
    52  	return nil
    53  }
    54  
    55  func (g *EscalationPolicyGenerator) InitResources() error {
    56  	client, err := g.Client()
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	funcs := []func(*pagerduty.Client) error{
    62  		g.createEscalationPolicyResources,
    63  	}
    64  
    65  	for _, f := range funcs {
    66  		err := f(client)
    67  		if err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	return nil
    73  }