github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/rabbitmq/binding.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 BindingGenerator struct {
    25  	RBTService
    26  }
    27  
    28  type Binding struct {
    29  	Source          string `json:"source"`
    30  	Vhost           string `json:"vhost"`
    31  	Destination     string `json:"destination"`
    32  	DestinationType string `json:"destination_type"`
    33  	PropertiesKey   string `json:"properties_key"`
    34  }
    35  
    36  type Bindings []Binding
    37  
    38  var BindingAllowEmptyValues = []string{"source"}
    39  var BindingAdditionalFields = map[string]interface{}{}
    40  
    41  func (g BindingGenerator) createResources(bindings Bindings) []terraformutils.Resource {
    42  	var resources []terraformutils.Resource
    43  	for _, binding := range bindings {
    44  		resources = append(resources, terraformutils.NewResource(
    45  			fmt.Sprintf("%s/%s/%s/%s/%s", binding.Vhost, binding.Source, binding.Destination, binding.DestinationType, binding.PropertiesKey),
    46  			fmt.Sprintf("binding_%s_%s_%s_%s", normalizeResourceName(binding.Source), normalizeResourceName(binding.Vhost), normalizeResourceName(binding.Destination), normalizeResourceName(binding.DestinationType)),
    47  			"rabbitmq_binding",
    48  			"rabbitmq",
    49  			map[string]string{
    50  				"source":           binding.Source,
    51  				"vhost":            binding.Vhost,
    52  				"destination":      binding.Destination,
    53  				"destination_type": binding.DestinationType,
    54  				"properties_key":   binding.PropertiesKey,
    55  			},
    56  			BindingAllowEmptyValues,
    57  			BindingAdditionalFields,
    58  		))
    59  	}
    60  	return resources
    61  }
    62  
    63  func (g *BindingGenerator) InitResources() error {
    64  	body, err := g.generateRequest("/api/bindings?columns=source,vhost,destination,destination_type,properties_key")
    65  	if err != nil {
    66  		return err
    67  	}
    68  	var bindings Bindings
    69  	err = json.Unmarshal(body, &bindings)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	g.Resources = g.createResources(bindings)
    74  	return nil
    75  }