|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/ovh/terraform-provider-ovh/ovh/helpers" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceDbaasLogsRolePermissionStream() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + CreateContext: resourceDbaasLogsRolePermissionStreamCreate, |
| 18 | + ReadContext: resourceDbaasLogsRolePermissionStreamRead, |
| 19 | + DeleteContext: resourceDbaasLogsRolePermissionStreamDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: resourceDbaasLogsRolePermissionStreamImportState, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "service_name": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Description: "Service name", |
| 27 | + Required: true, |
| 28 | + ForceNew: true, |
| 29 | + }, |
| 30 | + "role_id": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "Role ID to which the permission will be appended", |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + }, |
| 36 | + "stream_id": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "Graylog stream ID to be associated as a permission", |
| 39 | + Required: true, |
| 40 | + ForceNew: true, |
| 41 | + }, |
| 42 | + // Computed fields from the API GET response. |
| 43 | + "permission_id": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "Permission ID", |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "permission_type": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "Permission type (e.g., READ_ONLY)", |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// resourceDbaasLogsRolePermissionStreamCreate adds a stream permission to a role. |
| 58 | +func resourceDbaasLogsRolePermissionStreamCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 59 | + config := meta.(*Config) |
| 60 | + serviceName := d.Get("service_name").(string) |
| 61 | + roleId := d.Get("role_id").(string) |
| 62 | + streamId := d.Get("stream_id").(string) |
| 63 | + |
| 64 | + log.Printf("[DEBUG] Adding Graylog stream permission: service=%s, role=%s", serviceName, roleId) |
| 65 | + |
| 66 | + opts := (&DbaasLogsRolePermissionStreamOpts{}).FromResource(d) |
| 67 | + res := &DbaasLogsOperation{} |
| 68 | + endpoint := fmt.Sprintf("/dbaas/logs/%s/role/%s/permission/stream", url.PathEscape(serviceName), url.PathEscape(roleId)) |
| 69 | + if err := config.OVHClient.Post(endpoint, opts, res); err != nil { |
| 70 | + return diag.Errorf("Error calling POST %s:\n\t%q", endpoint, err) |
| 71 | + } |
| 72 | + |
| 73 | + // Wait for the asynchronous operation to complete. |
| 74 | + if _, err := waitForDbaasLogsOperation(ctx, config.OVHClient, serviceName, res.OperationId); err != nil { |
| 75 | + return diag.FromErr(err) |
| 76 | + } |
| 77 | + |
| 78 | + // Retrieve the full list of permission IDs for the role. |
| 79 | + listEndpoint := fmt.Sprintf("/dbaas/logs/%s/role/%s/permission", url.PathEscape(serviceName), url.PathEscape(roleId)) |
| 80 | + var permissionIDs []string |
| 81 | + if err := config.OVHClient.Get(listEndpoint, &permissionIDs); err != nil { |
| 82 | + return diag.Errorf("Error retrieving permissions list (%s):\n\t%q", listEndpoint, err) |
| 83 | + } |
| 84 | + |
| 85 | + // Iterate over each permission ID to retrieve full details and filter by streamId. |
| 86 | + var foundPermission *DbaasLogsRolePermissionStream |
| 87 | + for _, pid := range permissionIDs { |
| 88 | + permEndpoint := fmt.Sprintf("/dbaas/logs/%s/role/%s/permission/%s", url.PathEscape(serviceName), url.PathEscape(roleId), url.PathEscape(pid)) |
| 89 | + var permission DbaasLogsRolePermissionStream |
| 90 | + if err := config.OVHClient.Get(permEndpoint, &permission); err != nil { |
| 91 | + // Optionally, log error and continue with next permission. |
| 92 | + log.Printf("[WARN] Failed to get permission details for ID %s: %v", pid, err) |
| 93 | + continue |
| 94 | + } |
| 95 | + if permission.StreamId == streamId { |
| 96 | + foundPermission = &permission |
| 97 | + break |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if foundPermission == nil { |
| 102 | + return diag.Errorf("No permission found for streamId %s", streamId) |
| 103 | + } |
| 104 | + |
| 105 | + d.SetId(foundPermission.PermissionId) |
| 106 | + return resourceDbaasLogsRolePermissionStreamRead(ctx, d, meta) |
| 107 | +} |
| 108 | + |
| 109 | +// resourceDbaasLogsRolePermissionStreamRead reads the details of a permission. |
| 110 | +func resourceDbaasLogsRolePermissionStreamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 111 | + config := meta.(*Config) |
| 112 | + serviceName := d.Get("service_name").(string) |
| 113 | + roleId := d.Get("role_id").(string) |
| 114 | + permissionId := d.Id() |
| 115 | + |
| 116 | + log.Printf("[DEBUG] Reading permission: service=%s, role=%s, permission=%s", serviceName, roleId, permissionId) |
| 117 | + |
| 118 | + permission := &DbaasLogsRolePermissionStream{} |
| 119 | + endpoint := fmt.Sprintf("/dbaas/logs/%s/role/%s/permission/%s", url.PathEscape(serviceName), url.PathEscape(roleId), url.PathEscape(permissionId)) |
| 120 | + if err := config.OVHClient.Get(endpoint, permission); err != nil { |
| 121 | + return diag.FromErr(helpers.CheckDeleted(d, err, endpoint)) |
| 122 | + } |
| 123 | + |
| 124 | + d.Set("permission_id", permission.PermissionId) |
| 125 | + d.Set("permission_type", permission.PermissionType) |
| 126 | + d.Set("stream_id", permission.StreamId) |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +// resourceDbaasLogsRolePermissionStreamDelete deletes the specified permission. |
| 132 | +func resourceDbaasLogsRolePermissionStreamDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 133 | + config := meta.(*Config) |
| 134 | + serviceName := d.Get("service_name").(string) |
| 135 | + roleId := d.Get("role_id").(string) |
| 136 | + permissionId := d.Id() |
| 137 | + |
| 138 | + log.Printf("[DEBUG] Deleting permission: service=%s, role=%s, permission=%s", serviceName, roleId, permissionId) |
| 139 | + |
| 140 | + res := &DbaasLogsOperation{} |
| 141 | + endpoint := fmt.Sprintf("/dbaas/logs/%s/role/%s/permission/%s", url.PathEscape(serviceName), url.PathEscape(roleId), url.PathEscape(permissionId)) |
| 142 | + if err := config.OVHClient.Delete(endpoint, res); err != nil { |
| 143 | + return diag.FromErr(helpers.CheckDeleted(d, err, endpoint)) |
| 144 | + } |
| 145 | + |
| 146 | + // Wait for the asynchronous deletion operation to complete. |
| 147 | + if _, err := waitForDbaasLogsOperation(ctx, config.OVHClient, serviceName, res.OperationId); err != nil { |
| 148 | + return diag.FromErr(err) |
| 149 | + } |
| 150 | + |
| 151 | + d.SetId("") |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +// resourceDbaasLogsRolePermissionStreamImportState imports a permission given an ID in the format "service_name/role_id/permission_id". |
| 156 | +func resourceDbaasLogsRolePermissionStreamImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 157 | + givenID := d.Id() |
| 158 | + splitID := strings.SplitN(givenID, "/", 3) |
| 159 | + if len(splitID) != 3 { |
| 160 | + return nil, fmt.Errorf("Import ID must be in service_name/role_id/permission_id format") |
| 161 | + } |
| 162 | + serviceName := splitID[0] |
| 163 | + roleId := splitID[1] |
| 164 | + permissionId := splitID[2] |
| 165 | + |
| 166 | + d.Set("service_name", serviceName) |
| 167 | + d.Set("role_id", roleId) |
| 168 | + d.SetId(permissionId) |
| 169 | + |
| 170 | + return []*schema.ResourceData{d}, nil |
| 171 | +} |
0 commit comments