github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/rabbitmq/permissions.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 PermissionsGenerator struct {
    25  	RBTService
    26  }
    27  
    28  type Permissions struct {
    29  	User  string `json:"user"`
    30  	Vhost string `json:"vhost"`
    31  }
    32  
    33  type AllPermissions []Permissions
    34  
    35  var PermissionsAllowEmptyValues = []string{"configure", "write", "read"}
    36  var PermissionsAdditionalFields = map[string]interface{}{}
    37  
    38  func (g PermissionsGenerator) createResources(allPermissions AllPermissions) []terraformutils.Resource {
    39  	var resources []terraformutils.Resource
    40  	for _, permissions := range allPermissions {
    41  		resources = append(resources, terraformutils.NewResource(
    42  			fmt.Sprintf("%s@%s", permissions.User, permissions.Vhost),
    43  			fmt.Sprintf("permissions_%s_%s", normalizeResourceName(permissions.User), normalizeResourceName(permissions.Vhost)),
    44  			"rabbitmq_permissions",
    45  			"rabbitmq",
    46  			map[string]string{
    47  				"user":  permissions.User,
    48  				"vhost": permissions.Vhost,
    49  			},
    50  			PermissionsAllowEmptyValues,
    51  			PermissionsAdditionalFields,
    52  		))
    53  	}
    54  	return resources
    55  }
    56  
    57  func (g *PermissionsGenerator) InitResources() error {
    58  	body, err := g.generateRequest("/api/permissions?columns=user,vhost")
    59  	if err != nil {
    60  		return err
    61  	}
    62  	var permissions AllPermissions
    63  	err = json.Unmarshal(body, &permissions)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	g.Resources = g.createResources(permissions)
    68  	return nil
    69  }