|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package cloudstack |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + |
| 26 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 29 | +) |
| 30 | + |
| 31 | +func resourceCloudStackRolePermission() *schema.Resource { |
| 32 | + return &schema.Resource{ |
| 33 | + Create: resourceCloudStackRolePermissionCreate, |
| 34 | + Read: resourceCloudStackRolePermissionRead, |
| 35 | + Update: resourceCloudStackRolePermissionUpdate, |
| 36 | + Delete: resourceCloudStackRolePermissionDelete, |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "role_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + Description: "ID of the role the permission (rule) belongs to.", |
| 43 | + }, |
| 44 | + "rule": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + Description: "The API name or wildcard (e.g. 'list*') the permission applies to.", |
| 49 | + }, |
| 50 | + "permission": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + ValidateFunc: validation.StringInSlice([]string{"allow", "deny"}, false), |
| 54 | + Description: "Whether the rule is allowed or denied. Valid options are: allow, deny.", |
| 55 | + }, |
| 56 | + "description": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + ForceNew: true, |
| 60 | + Description: "A description for the role permission.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceCloudStackRolePermissionCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + cs := meta.(*cloudstack.CloudStackClient) |
| 68 | + |
| 69 | + roleID := d.Get("role_id").(string) |
| 70 | + rule := d.Get("rule").(string) |
| 71 | + permission := d.Get("permission").(string) |
| 72 | + |
| 73 | + // Create a new parameter struct |
| 74 | + p := cs.Role.NewCreateRolePermissionParams(permission, roleID, rule) |
| 75 | + |
| 76 | + if description, ok := d.GetOk("description"); ok { |
| 77 | + p.SetDescription(description.(string)) |
| 78 | + } |
| 79 | + |
| 80 | + log.Printf("[DEBUG] Creating Role Permission %s (%s) for role %s", rule, permission, roleID) |
| 81 | + r, err := cs.Role.CreateRolePermission(p) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("Error creating Role Permission: %s", err) |
| 85 | + } |
| 86 | + |
| 87 | + log.Printf("[DEBUG] Role Permission %s successfully created", rule) |
| 88 | + d.SetId(r.Id) |
| 89 | + |
| 90 | + return resourceCloudStackRolePermissionRead(d, meta) |
| 91 | +} |
| 92 | + |
| 93 | +func resourceCloudStackRolePermissionRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + cs := meta.(*cloudstack.CloudStackClient) |
| 95 | + |
| 96 | + roleID := d.Get("role_id").(string) |
| 97 | + |
| 98 | + // The API only supports listing permissions by role, so fetch them all |
| 99 | + // and locate the one matching this resource's ID. |
| 100 | + p := cs.Role.NewListRolePermissionsParams() |
| 101 | + p.SetRoleid(roleID) |
| 102 | + |
| 103 | + l, err := cs.Role.ListRolePermissions(p) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("Error listing Role Permissions: %s", err) |
| 106 | + } |
| 107 | + |
| 108 | + for _, rp := range l.RolePermissions { |
| 109 | + if rp.Id == d.Id() { |
| 110 | + d.Set("role_id", rp.Roleid) |
| 111 | + d.Set("rule", rp.Rule) |
| 112 | + d.Set("permission", rp.Permission) |
| 113 | + d.Set("description", rp.Description) |
| 114 | + return nil |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + log.Printf("[DEBUG] Role Permission %s no longer exists", d.Id()) |
| 119 | + d.SetId("") |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func resourceCloudStackRolePermissionUpdate(d *schema.ResourceData, meta interface{}) error { |
| 125 | + cs := meta.(*cloudstack.CloudStackClient) |
| 126 | + |
| 127 | + // Only the permission (allow/deny) can be changed in place; the role_id, |
| 128 | + // rule and description are all ForceNew. |
| 129 | + p := cs.Role.NewUpdateRolePermissionParams(d.Get("role_id").(string)) |
| 130 | + p.SetRuleid(d.Id()) |
| 131 | + p.SetPermission(d.Get("permission").(string)) |
| 132 | + |
| 133 | + log.Printf("[DEBUG] Updating Role Permission %s", d.Id()) |
| 134 | + _, err := cs.Role.UpdateRolePermission(p) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("Error updating Role Permission: %s", err) |
| 138 | + } |
| 139 | + |
| 140 | + return resourceCloudStackRolePermissionRead(d, meta) |
| 141 | +} |
| 142 | + |
| 143 | +func resourceCloudStackRolePermissionDelete(d *schema.ResourceData, meta interface{}) error { |
| 144 | + cs := meta.(*cloudstack.CloudStackClient) |
| 145 | + |
| 146 | + // Create a new parameter struct |
| 147 | + p := cs.Role.NewDeleteRolePermissionParams(d.Id()) |
| 148 | + |
| 149 | + log.Printf("[DEBUG] Deleting Role Permission %s", d.Id()) |
| 150 | + _, err := cs.Role.DeleteRolePermission(p) |
| 151 | + |
| 152 | + if err != nil { |
| 153 | + return fmt.Errorf("Error deleting Role Permission: %s", err) |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
0 commit comments