github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/rabbitmq/policy.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 rabbitmq
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  )
    23  
    24  type PolicyGenerator struct {
    25  	RBTService
    26  }
    27  
    28  type Policy struct {
    29  	Name  string `json:"name"`
    30  	Vhost string `json:"vhost"`
    31  }
    32  
    33  type Policies []Policy
    34  
    35  var PolicyAllowEmptyValues = []string{}
    36  var PolicyAdditionalFields = map[string]interface{}{}
    37  
    38  func (g PolicyGenerator) createResources(policies Policies) []terraformutils.Resource {
    39  	var resources []terraformutils.Resource
    40  	for _, policy := range policies {
    41  		resources = append(resources, terraformutils.NewResource(
    42  			fmt.Sprintf("%s@%s", policy.Name, policy.Vhost),
    43  			fmt.Sprintf("policy_%s_%s", normalizeResourceName(policy.Vhost), normalizeResourceName(policy.Name)),
    44  			"rabbitmq_policy",
    45  			"rabbitmq",
    46  			map[string]string{
    47  				"name":  policy.Name,
    48  				"vhost": policy.Vhost,
    49  			},
    50  			PolicyAllowEmptyValues,
    51  			PolicyAdditionalFields,
    52  		))
    53  	}
    54  	return resources
    55  }
    56  
    57  func (g *PolicyGenerator) InitResources() error {
    58  	body, err := g.generateRequest("/api/policies?columns=name,vhost")
    59  	if err != nil {
    60  		return err
    61  	}
    62  	var policies Policies
    63  	err = json.Unmarshal(body, &policies)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	g.Resources = g.createResources(policies)
    68  	return nil
    69  }