|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/ovh/terraform-provider-ovh/ovh/helpers" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceCloudProjectDatabasepPostgresqlUser() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceCloudProjectDatabasePostgresqlUserCreate, |
| 16 | + Read: resourceCloudProjectDatabasePostgresqlUserRead, |
| 17 | + Delete: resourceCloudProjectDatabasePostgresqlUserDelete, |
| 18 | + Update: resourceCloudProjectDatabasePostgresqlUserUpdate, |
| 19 | + |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: resourceCloudProjectDatabasePostgresqlUserImportState, |
| 22 | + }, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "service_name": { |
| 26 | + Type: schema.TypeString, |
| 27 | + ForceNew: true, |
| 28 | + Required: true, |
| 29 | + DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil), |
| 30 | + }, |
| 31 | + "cluster_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Description: "Id of the database cluster", |
| 34 | + ForceNew: true, |
| 35 | + Required: true, |
| 36 | + }, |
| 37 | + "name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Description: "Name of the user", |
| 40 | + ForceNew: true, |
| 41 | + Required: true, |
| 42 | + }, |
| 43 | + "roles": { |
| 44 | + Type: schema.TypeList, |
| 45 | + Description: "Roles the user belongs to", |
| 46 | + Optional: true, |
| 47 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 48 | + }, |
| 49 | + |
| 50 | + //Computed |
| 51 | + "created_at": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Description: "Date of the creation of the cluster", |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "password": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Password of the user", |
| 59 | + Sensitive: true, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "status": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Current status of the user", |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func resourceCloudProjectDatabasePostgresqlUserImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 72 | + givenId := d.Id() |
| 73 | + splitId := strings.SplitN(givenId, "/", 3) |
| 74 | + if len(splitId) != 3 { |
| 75 | + return nil, fmt.Errorf("Import Id is not service_name/cluster_id/id formatted") |
| 76 | + } |
| 77 | + serviceName := splitId[0] |
| 78 | + clusterId := splitId[1] |
| 79 | + id := splitId[2] |
| 80 | + d.SetId(id) |
| 81 | + d.Set("cluster_id", clusterId) |
| 82 | + d.Set("service_name", serviceName) |
| 83 | + |
| 84 | + results := make([]*schema.ResourceData, 1) |
| 85 | + results[0] = d |
| 86 | + return results, nil |
| 87 | +} |
| 88 | + |
| 89 | +func resourceCloudProjectDatabasePostgresqlUserCreate(d *schema.ResourceData, meta interface{}) error { |
| 90 | + config := meta.(*Config) |
| 91 | + serviceName := d.Get("service_name").(string) |
| 92 | + clusterId := d.Get("cluster_id").(string) |
| 93 | + |
| 94 | + endpoint := fmt.Sprintf("/cloud/project/%s/database/postgresql/%s/user", |
| 95 | + url.PathEscape(serviceName), |
| 96 | + url.PathEscape(clusterId), |
| 97 | + ) |
| 98 | + params := (&CloudProjectDatabasePostgresqlUserCreateOpts{}).FromResource(d) |
| 99 | + res := &CloudProjectDatabasePostgresqlUserResponse{} |
| 100 | + |
| 101 | + log.Printf("[DEBUG] Will create user: %+v for cluster %s from project %s", params, clusterId, serviceName) |
| 102 | + err := config.OVHClient.Post(endpoint, params, res) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("calling Post %s with params %s:\n\t %q", endpoint, params, err) |
| 105 | + } |
| 106 | + |
| 107 | + d.SetId(res.Id) |
| 108 | + d.Set("password", res.Password) |
| 109 | + |
| 110 | + return resourceCloudProjectDatabasePostgresqlUserRead(d, meta) |
| 111 | +} |
| 112 | + |
| 113 | +func resourceCloudProjectDatabasePostgresqlUserRead(d *schema.ResourceData, meta interface{}) error { |
| 114 | + config := meta.(*Config) |
| 115 | + serviceName := d.Get("service_name").(string) |
| 116 | + clusterId := d.Get("cluster_id").(string) |
| 117 | + id := d.Id() |
| 118 | + |
| 119 | + endpoint := fmt.Sprintf("/cloud/project/%s/database/postgresql/%s/user/%s", |
| 120 | + url.PathEscape(serviceName), |
| 121 | + url.PathEscape(clusterId), |
| 122 | + url.PathEscape(id), |
| 123 | + ) |
| 124 | + res := &CloudProjectDatabasePostgresqlUserResponse{} |
| 125 | + |
| 126 | + log.Printf("[DEBUG] Will read user %s from cluster %s from project %s", id, clusterId, serviceName) |
| 127 | + if err := config.OVHClient.Get(endpoint, res); err != nil { |
| 128 | + return helpers.CheckDeleted(d, err, endpoint) |
| 129 | + } |
| 130 | + |
| 131 | + for k, v := range res.ToMap() { |
| 132 | + if k != "id" { |
| 133 | + d.Set(k, v) |
| 134 | + } else { |
| 135 | + d.SetId(fmt.Sprint(v)) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + log.Printf("[DEBUG] Read user %+v", res) |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func resourceCloudProjectDatabasePostgresqlUserUpdate(d *schema.ResourceData, meta interface{}) error { |
| 144 | + config := meta.(*Config) |
| 145 | + serviceName := d.Get("service_name").(string) |
| 146 | + clusterId := d.Get("cluster_id").(string) |
| 147 | + id := d.Id() |
| 148 | + |
| 149 | + endpoint := fmt.Sprintf("/cloud/project/%s/database/postgresql/%s/user/%s", |
| 150 | + url.PathEscape(serviceName), |
| 151 | + url.PathEscape(clusterId), |
| 152 | + url.PathEscape(id), |
| 153 | + ) |
| 154 | + params := (&CloudProjectDatabasePostgresqlUserUpdateOpts{}).FromResource(d) |
| 155 | + |
| 156 | + log.Printf("[DEBUG] Will update user: %+v from cluster %s from project %s", params, clusterId, serviceName) |
| 157 | + err := config.OVHClient.Put(endpoint, params, nil) |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("calling Put %s with params %v:\n\t %q", endpoint, params, err) |
| 160 | + } |
| 161 | + |
| 162 | + return resourceCloudProjectDatabasePostgresqlUserRead(d, meta) |
| 163 | +} |
| 164 | + |
| 165 | +func resourceCloudProjectDatabasePostgresqlUserDelete(d *schema.ResourceData, meta interface{}) error { |
| 166 | + config := meta.(*Config) |
| 167 | + serviceName := d.Get("service_name").(string) |
| 168 | + clusterId := d.Get("cluster_id").(string) |
| 169 | + id := d.Id() |
| 170 | + |
| 171 | + endpoint := fmt.Sprintf("/cloud/project/%s/database/postgresql/%s/user/%s", |
| 172 | + url.PathEscape(serviceName), |
| 173 | + url.PathEscape(clusterId), |
| 174 | + url.PathEscape(id), |
| 175 | + ) |
| 176 | + |
| 177 | + log.Printf("[DEBUG] Will delete useruser %s from cluster %s from project %s", id, clusterId, serviceName) |
| 178 | + err := config.OVHClient.Delete(endpoint, nil) |
| 179 | + if err != nil { |
| 180 | + return helpers.CheckDeleted(d, err, endpoint) |
| 181 | + } |
| 182 | + |
| 183 | + d.SetId("") |
| 184 | + |
| 185 | + return nil |
| 186 | +} |
0 commit comments