github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/rabbitmq/exchange.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 ExchangeGenerator struct {
    25  	RBTService
    26  }
    27  
    28  type Exchange struct {
    29  	Name  string `json:"name"`
    30  	Vhost string `json:"vhost"`
    31  }
    32  
    33  type Exchanges []Exchange
    34  
    35  var ExchangeAllowEmptyValues = []string{}
    36  var ExchangeAdditionalFields = map[string]interface{}{}
    37  
    38  func (g ExchangeGenerator) createResources(exchanges Exchanges) []terraformutils.Resource {
    39  	var resources []terraformutils.Resource
    40  	for _, exchange := range exchanges {
    41  		if len(exchange.Name) == 0 {
    42  			continue
    43  		}
    44  		resources = append(resources, terraformutils.NewResource(
    45  			fmt.Sprintf("%s@%s", exchange.Name, exchange.Vhost),
    46  			fmt.Sprintf("exchange_%s_%s", normalizeResourceName(exchange.Vhost), normalizeResourceName(exchange.Name)),
    47  			"rabbitmq_exchange",
    48  			"rabbitmq",
    49  			map[string]string{
    50  				"name":  exchange.Name,
    51  				"vhost": exchange.Vhost,
    52  			},
    53  			ExchangeAllowEmptyValues,
    54  			ExchangeAdditionalFields,
    55  		))
    56  	}
    57  	return resources
    58  }
    59  
    60  func (g *ExchangeGenerator) InitResources() error {
    61  	body, err := g.generateRequest("/api/exchanges?columns=name,vhost")
    62  	if err != nil {
    63  		return err
    64  	}
    65  	var exchanges Exchanges
    66  	err = json.Unmarshal(body, &exchanges)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	g.Resources = g.createResources(exchanges)
    71  	return nil
    72  }