github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/rabbitmq/user.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  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  )
    22  
    23  type UserGenerator struct {
    24  	RBTService
    25  }
    26  
    27  type User struct {
    28  	Name string `json:"name"`
    29  }
    30  
    31  type Users []User
    32  
    33  var UserAllowEmptyValues = []string{}
    34  
    35  func (g UserGenerator) createResources(users Users) []terraformutils.Resource {
    36  	var resources []terraformutils.Resource
    37  	for _, user := range users {
    38  		resources = append(resources, terraformutils.NewSimpleResource(
    39  			user.Name,
    40  			"user_"+normalizeResourceName(user.Name),
    41  			"rabbitmq_user",
    42  			"rabbitmq",
    43  			UserAllowEmptyValues,
    44  		))
    45  	}
    46  	return resources
    47  }
    48  
    49  func (g *UserGenerator) InitResources() error {
    50  	body, err := g.generateRequest("/api/users?columns=name")
    51  	if err != nil {
    52  		return err
    53  	}
    54  	var users Users
    55  	err = json.Unmarshal(body, &users)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	g.Resources = g.createResources(users)
    60  	return nil
    61  }