Skip to content

Commit

Permalink
Support: ICD configuration IBM-Cloud#3278 IBM-Cloud#1428
Browse files Browse the repository at this point in the history
  • Loading branch information
kavya498 committed Nov 11, 2021
1 parent 7f65dc4 commit 5713bb0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/IBM-Cloud/terraform-provider-ibm
go 1.16

require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20211005115032-00ba77f0db57
github.com/IBM-Cloud/bluemix-go v0.0.0-20211109110327-8ef3d89514c8
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20210705152127-41ca00fc9a62
github.com/IBM-Cloud/power-go-client v1.0.76
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/IBM-Cloud/bluemix-go v0.0.0-20211005115032-00ba77f0db57 h1:TTNIFMCfTO30MLvEpkhcDM9MTLHuBNz0//DnVTVX3JY=
github.com/IBM-Cloud/bluemix-go v0.0.0-20211005115032-00ba77f0db57/go.mod h1:q0fXFSbum/16D8Mgn1ROSfSyX4BmvBCm/hHdcXz0wCU=
github.com/IBM-Cloud/bluemix-go v0.0.0-20211109110327-8ef3d89514c8 h1:LxFLUwpf7FzaucTZ8HXld1Q2e/lS/OB5t44//guZCvM=
github.com/IBM-Cloud/bluemix-go v0.0.0-20211109110327-8ef3d89514c8/go.mod h1:q0fXFSbum/16D8Mgn1ROSfSyX4BmvBCm/hHdcXz0wCU=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20210705152127-41ca00fc9a62 h1:MOkcr6qQGk4tY542ZJ1DggVh2WUP72EEyLB79llFVH8=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20210705152127-41ca00fc9a62/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
Expand Down
20 changes: 20 additions & 0 deletions ibm/data_source_ibm_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ibm

import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
Expand Down Expand Up @@ -513,6 +514,11 @@ func dataSourceIBMDatabaseInstance() *schema.Resource {
},
},
},
"configuration_schema": {
Type: schema.TypeString,
Computed: true,
Description: "The configuration schema in JSON format",
},
ResourceName: {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -738,6 +744,20 @@ func dataSourceIBMDatabaseInstanceRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error writing certificate to file: %s", err)
}
d.Set("cert_file_path", certFile)
if serviceOff == "databases-for-postgresql" || serviceOff == "databases-for-redis" || serviceOff == "databases-for-enterprisedb" {
configSchema, err := icdClient.Configurations().GetConfiguration(icdId)
if err != nil {
return fmt.Errorf("[ERROR] Error getting database (%s) configuration schema : %s", icdId, err)
}
s, err := json.Marshal(configSchema)
if err != nil {
return fmt.Errorf("error marshalling the database configuration schema: %s", err)
}

if err = d.Set("configuration_schema", string(s)); err != nil {
return fmt.Errorf("error setting the database configuration schema: %s", err)
}
}

return nil
}
54 changes: 54 additions & 0 deletions ibm/resource_ibm_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ func resourceIBMDatabaseInstance() *schema.Resource {
// return true
// },
},
"configuration": {
Type: schema.TypeString,
Optional: true,
StateFunc: func(v interface{}) string {
json, err := normalizeJSONString(v)
if err != nil {
return fmt.Sprintf("%q", err.Error())
}
return json
},
Description: "The configuration in JSON format",
},
"configuration_schema": {
Type: schema.TypeString,
Computed: true,
Description: "The configuration schema in JSON format",
},
"version": {
Description: "The database version to provision if specified",
Type: schema.TypeString,
Expand Down Expand Up @@ -1403,6 +1420,20 @@ func resourceIBMDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
}
d.Set("connectionstrings", flattenConnectionStrings(connectionStrings))

if serviceOff == "databases-for-postgresql" || serviceOff == "databases-for-redis" || serviceOff == "databases-for-enterprisedb"{
configSchema, err := icdClient.Configurations().GetConfiguration(icdId)
if err != nil {
return fmt.Errorf("[ERROR] Error getting database (%s) configuration schema : %s", icdId, err)
}
s, err := json.Marshal(configSchema)
if err != nil {
return fmt.Errorf("error marshalling the database configuration schema: %s", err)
}

if err = d.Set("configuration_schema", string(s)); err != nil {
return fmt.Errorf("error setting the database configuration schema: %s", err)
}
}
return nil
}

Expand Down Expand Up @@ -1468,6 +1499,29 @@ func resourceIBMDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
return err
}
}
if d.HasChange("configuration") {
service := d.Get("service").(string)
if service == "databases-for-postgresql" || service == "databases-for-redis" || service == "databases-for-enterprisedb" {
if s, ok := d.GetOk("configuration"); ok {
var configuration interface{}
json.Unmarshal([]byte(s.(string)), &configuration)
configPayload := icdv4.ConfigurationReq{Configuration: configuration}
task, err := icdClient.Configurations().UpdateConfiguration(icdId, configPayload)
if err != nil {
return fmt.Errorf("[ERROR] Error updating database (%s) configuration: %s", icdId, err)
}
_, err = waitForDatabaseTaskComplete(task.Id, d, meta, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf(
"[ERROR] Error waiting for database (%s) configuration update task to complete: %s", icdId, err)
}
}

} else {
return fmt.Errorf("[ERROR] given database type %s is not configurable", service)
}

}

if d.HasChange("members_memory_allocation_mb") || d.HasChange("members_disk_allocation_mb") || d.HasChange("members_cpu_allocation_count") || d.HasChange("node_memory_allocation_mb") || d.HasChange("node_disk_allocation_mb") || d.HasChange("node_cpu_allocation_count") {
params := icdv4.GroupReq{}
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ In addition to all argument references list, you can access the following attrib
- `adminuser` - (String) The user ID of the default administration user for the database, such as `admin` or `root`.
- `cert_file_path` - (String) The absolute path to certificate PEM file.
- `connectionstrings` (List) List of connection strings by userid for the database. For information about how to use connection strings, see the [documentation](https://cloud.ibm.com/docs/databases-for-postgresql?topic=databases-for-postgresql-connection-strings). The results are returned in pairs of the userid and string: `connectionstrings.1.name = admin connectionstrings.1.string = postgres://admin:$PASSWORD@12345aa1-1111-1111-a1aa-a1aaa11aa1a1.a1a1a111a1a11a1a111a111a1a111a111.databases.appdomain.cloud:32554/ibmclouddb?sslmode=verify-full`.
- `configuration_schema` (String) Database Configuration Schema in JSON format.
- `id` - (String) The CRN of the IBM Cloud Databases instance.
- `guid` - (String) The unique identifier of the IBM Cloud Databases instance.
- `plan` - (String) The service plan of the IBM Cloud Databases instance.
Expand Down
25 changes: 25 additions & 0 deletions website/docs/r/database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,29 @@ resource "ibm_database" "edb" {
delete = "15m"
}
}
```
### Updating configuration for postgres database

```terraform
data "ibm_resource_group" "test_acc" {
is_default = true
}
resource "ibm_database" "db" {
location = "us-east"
members_cpu_allocation_count = 0
members_disk_allocation_mb = 10240
members_memory_allocation_mb = 2048
name = "telus-database"
service = "databases-for-postgresql"
plan = "standard"
configuration = <<CONFIGURATION
{
"max_connections": 400
}
CONFIGURATION
}
```

**provider.tf**
Expand Down Expand Up @@ -310,6 +333,7 @@ Review the argument reference that you can specify for your resource.
- `rate_units` - (Optional, String) Auto scaling rate in units.
- `backup_id` - (Optional, String) The CRN of a backup resource to restore from. The backup is created by a database deployment with the same service ID. The backup is loaded after provisioning and the new deployment starts up that uses that data. A backup CRN is in the format `crn:v1:<…>:backup:`. If omitted, the database is provisioned empty.
- `backup_encryption_key_crn`- (Optional, Forces new resource, String) The CRN of a key protect key, that you want to use for encrypting disk that holds deployment backups. A key protect CRN is in the format `crn:v1:<...>:key:`. Backup_encryption_key_crn can be added only at the time of creation and no update support are available.
- `configuration` - (Optional, Json String) Database Configuration in JSON format. Supported services `databases-for-postgresql`, `databases-for-redis` and `databases-for-enterprisedb`. For valid values please refer [API docs](https://cloud.ibm.com/apidocs/cloud-databases-api/cloud-databases-api-v4#setdatabaseconfiguration-request).
- `guid` - (Optional, String) The unique identifier of the database instance.
- `key_protect_key` - (Optional, Forces new resource, String) The root key CRN of a Key Management Services like Key Protect or Hyper Protect Crypto Service (HPCS) that you want to use for disk encryption. A key CRN is in the format `crn:v1:<…>:key:`. You can specify the root key during the database creation only. After the database is created, you cannot update the root key. For more information, refer [Disk encryption](https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-key-protect#using-the-key-protect-key) documentation.
- `key_protect_instance` - (Optional, Forces new resource, String) The instance CRN of a Key Management Services like Key Protect or Hyper Protect Crypto Service (HPCS) that you want to use for disk encryption. An instance CRN is in the format `crn:v1:<…>::`.
Expand Down Expand Up @@ -350,6 +374,7 @@ Review the argument reference that you can specify for your resource.
In addition to all argument references list, you can access the following attribute references after your resource is created.

- `adminuser` - (String) The user ID of the database administrator. Example, `admin` or `root`.
- `configuration_schema` (String) Database Configuration Schema in JSON format.
- `connectionstrings` - (Array) A list of connection strings for the database for each user ID. For more information, about how to use connection strings, see the [documentation](https://cloud.ibm.com/docs/databases-for-postgresql?topic=databases-for-postgresql-connection-strings). The results are returned in pairs of the userid and string: `connectionstrings.1.name = admin connectionstrings.1.string = postgres://admin:$PASSWORD@79226bd4-4076-4873-b5ce-b1dba48ff8c4.b8a5e798d2d04f2e860e54e5d042c915.databases.appdomain.cloud:32554/ibmclouddb?sslmode=verify-full` Individual string parameters can be retrieved by using Terraform variables and outputs `connectionstrings.x.hosts.x.port` and `connectionstrings.x.hosts.x.host`.
- `id` - (String) The CRN of the database instance.
- `status` - (String) The status of the instance.
Expand Down

0 comments on commit 5713bb0

Please sign in to comment.