|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceCloudProjectDatabase() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: dataSourceCloudProjectDatabaseRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "service_name": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil), |
| 19 | + }, |
| 20 | + "engine": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Description: "Name of the engine of the service", |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "cluster_id": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Description: "Cluster ID", |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + |
| 31 | + //Computed |
| 32 | + "backup_time": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Description: "Time on which backups start every day", |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "created_at": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Description: "Date of the creation of the cluster", |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "description": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Description: "Description of the cluster", |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "endpoints": { |
| 48 | + Type: schema.TypeList, |
| 49 | + Description: "List of all endpoints of the service", |
| 50 | + Computed: true, |
| 51 | + Elem: &schema.Resource{ |
| 52 | + Schema: map[string]*schema.Schema{ |
| 53 | + "component": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "Type of component the URI relates to", |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "domain": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Domain of the cluster", |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "path": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Description: "Path of the endpoint", |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "port": { |
| 69 | + Type: schema.TypeInt, |
| 70 | + Description: "Connection port for the endpoint", |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + "scheme": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Description: "Scheme used to generate the URI", |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "ssl": { |
| 79 | + Type: schema.TypeBool, |
| 80 | + Description: "Defines whether the endpoint uses SSL", |
| 81 | + Computed: true, |
| 82 | + }, |
| 83 | + "ssl_mode": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Description: "SSL mode used to connect to the service if the SSL is enabled", |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + "uri": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Description: "URI of the endpoint", |
| 91 | + Computed: true, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + "flavor": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Description: "The node flavor used for this cluster", |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + "maintenance_time": { |
| 102 | + Type: schema.TypeString, |
| 103 | + Description: "Time on which maintenances can start every day", |
| 104 | + Computed: true, |
| 105 | + }, |
| 106 | + "network_type": { |
| 107 | + Type: schema.TypeString, |
| 108 | + Description: "Type of network of the cluster", |
| 109 | + Computed: true, |
| 110 | + }, |
| 111 | + "nodes": { |
| 112 | + Type: schema.TypeList, |
| 113 | + Description: "List of nodes composing the service", |
| 114 | + Computed: true, |
| 115 | + Elem: &schema.Resource{ |
| 116 | + Schema: map[string]*schema.Schema{ |
| 117 | + "network_id": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Description: "Private network ID in which the node is", |
| 120 | + Computed: true, |
| 121 | + }, |
| 122 | + "region": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Description: "Region of the node", |
| 125 | + Computed: true, |
| 126 | + }, |
| 127 | + "subnet_id": { |
| 128 | + Type: schema.TypeString, |
| 129 | + Description: "Private subnet ID in which the node is", |
| 130 | + Computed: true, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + "plan": { |
| 136 | + Type: schema.TypeString, |
| 137 | + Description: "Plan of the cluster", |
| 138 | + Computed: true, |
| 139 | + }, |
| 140 | + "status": { |
| 141 | + Type: schema.TypeString, |
| 142 | + Description: "Current status of the cluster", |
| 143 | + Computed: true, |
| 144 | + }, |
| 145 | + "version": { |
| 146 | + Type: schema.TypeString, |
| 147 | + Description: "Version of the engine deployed on the cluster", |
| 148 | + Computed: true, |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func dataSourceCloudProjectDatabaseRead(d *schema.ResourceData, meta interface{}) error { |
| 155 | + config := meta.(*Config) |
| 156 | + serviceName := d.Get("service_name").(string) |
| 157 | + engine := d.Get("engine").(string) |
| 158 | + id := d.Get("cluster_id").(string) |
| 159 | + |
| 160 | + serviceEndpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s", |
| 161 | + url.PathEscape(serviceName), |
| 162 | + url.PathEscape(engine), |
| 163 | + url.PathEscape(id), |
| 164 | + ) |
| 165 | + res := &CloudProjectDatabaseResponse{} |
| 166 | + |
| 167 | + log.Printf("[DEBUG] Will read database %s from project: %s", id, serviceName) |
| 168 | + if err := config.OVHClient.Get(serviceEndpoint, res); err != nil { |
| 169 | + return fmt.Errorf("Error calling GET %s:\n\t %q", serviceEndpoint, err) |
| 170 | + } |
| 171 | + |
| 172 | + nodesEndpoint := fmt.Sprintf("%s/node", serviceEndpoint) |
| 173 | + nodeList := &[]string{} |
| 174 | + if err := config.OVHClient.Get(nodesEndpoint, nodeList); err != nil { |
| 175 | + return fmt.Errorf("unable to get database %s nodes: %v", res.Id, err) |
| 176 | + } |
| 177 | + |
| 178 | + if len(*nodeList) == 0 { |
| 179 | + return fmt.Errorf("no node found for database %s", res.Id) |
| 180 | + } |
| 181 | + nodeEndpoint := fmt.Sprintf("%s/%s", nodesEndpoint, url.PathEscape((*nodeList)[0])) |
| 182 | + node := &CloudProjectDatabaseNodes{} |
| 183 | + if err := config.OVHClient.Get(nodeEndpoint, node); err != nil { |
| 184 | + return fmt.Errorf("unable to get database %s node %s: %v", res.Id, (*nodeList)[0], err) |
| 185 | + } |
| 186 | + |
| 187 | + res.Region = node.Region |
| 188 | + |
| 189 | + for k, v := range res.ToMap() { |
| 190 | + if k != "id" { |
| 191 | + d.Set(k, v) |
| 192 | + } else { |
| 193 | + d.SetId(fmt.Sprint(v)) |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + log.Printf("[DEBUG] Read Database %+v", res) |
| 198 | + return nil |
| 199 | +} |
0 commit comments