|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "regexp" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | +) |
| 19 | + |
| 20 | +type ConnectionConfig struct { |
| 21 | + ConnectionName types.String `tfsdk:"connection_name"` |
| 22 | + Database types.String `tfsdk:"database"` |
| 23 | + Username types.String `tfsdk:"username"` |
| 24 | + Password types.String `tfsdk:"password"` |
| 25 | + Proxy types.String `tfsdk:"proxy"` |
| 26 | + PrivateIP types.Bool `tfsdk:"private_ip"` |
| 27 | + PSC types.Bool `tfsdk:"psc"` |
| 28 | + SslMode types.String `tfsdk:"ssl_mode"` |
| 29 | + // IAMAuthentication types.Bool `tfsdk:"iam_authentication"` # Not supporting IAM authentication for now. |
| 30 | +} |
| 31 | + |
| 32 | +func (c *ConnectionConfig) Dsn() string { |
| 33 | + sslMode := "disable" |
| 34 | + if !c.SslMode.IsNull() { |
| 35 | + sslMode = c.SslMode.ValueString() |
| 36 | + } |
| 37 | + |
| 38 | + return fmt.Sprintf("host=%s dbname=%s user=%s password=%s sslmode=%s", |
| 39 | + c.ConnectionName.ValueString(), |
| 40 | + c.Database.ValueString(), |
| 41 | + c.Username.ValueString(), |
| 42 | + c.Password.ValueString(), |
| 43 | + sslMode) |
| 44 | +} |
| 45 | + |
| 46 | +func (c *ConnectionConfig) Id() string { |
| 47 | + var buf bytes.Buffer |
| 48 | + encoder := base64.NewEncoder(base64.StdEncoding, &buf) |
| 49 | + json.NewEncoder(encoder).Encode(c) |
| 50 | + encoder.Close() |
| 51 | + return buf.String() |
| 52 | +} |
| 53 | + |
| 54 | +func connectionConfigSchemaAttribute() schema.Attribute { |
| 55 | + return schema.SingleNestedAttribute{ |
| 56 | + Description: "The connection properties for the Cloud SQL instance.", |
| 57 | + MarkdownDescription: "The connection properties for the Cloud SQL instance.", |
| 58 | + Required: true, |
| 59 | + Attributes: map[string]schema.Attribute{ |
| 60 | + "connection_name": schema.StringAttribute{ |
| 61 | + MarkdownDescription: "The connection name of the Google Cloud SQL Postgresql instance. The `connection_name` format should be `<project>:<region>:<instance>`", |
| 62 | + Description: "The connection name of the Google Cloud SQL Postgresql instance. The connection_name format should be <project>:<region>:<instance>", |
| 63 | + Optional: true, |
| 64 | + Validators: []validator.String{ |
| 65 | + stringvalidator.RegexMatches(regexp.MustCompile(`^[a-z0-9\-]+\:[a-z0-9\-]+\:[a-z0-9\-]+$`), |
| 66 | + "`connection_name` must have the format of `<project>:<region>:<instance>`"), |
| 67 | + }, |
| 68 | + }, |
| 69 | + "database": schema.StringAttribute{ |
| 70 | + Description: "The database to connect to. Defaults to `postgres`.", |
| 71 | + MarkdownDescription: "The database to connect to. Defaults to `postgres`.", |
| 72 | + Optional: true, |
| 73 | + Computed: true, |
| 74 | + Default: stringdefault.StaticString("postgres"), |
| 75 | + PlanModifiers: []planmodifier.String{ |
| 76 | + stringplanmodifier.RequiresReplace(), |
| 77 | + }, |
| 78 | + }, |
| 79 | + "username": schema.StringAttribute{ |
| 80 | + MarkdownDescription: "The username to use to authenticate with the Cloud SQL Postgresql instance", |
| 81 | + Description: "The username to use to authenticate with the Cloud SQL Postgresql instance", |
| 82 | + Required: true, |
| 83 | + }, |
| 84 | + "password": schema.StringAttribute{ |
| 85 | + MarkdownDescription: "The password to use to authenticate using the built-in database authentication", |
| 86 | + Description: "The password to use to authenticate using the built-in database authentication", |
| 87 | + Required: true, |
| 88 | + Sensitive: true, |
| 89 | + }, |
| 90 | + "proxy": schema.StringAttribute{ |
| 91 | + MarkdownDescription: "Proxy socks url if used. Format needs to be `socks5://<ip>:<port>`", |
| 92 | + Description: "Proxy socks url if used. Format needs to be socks5://<ip>:<port>", |
| 93 | + Optional: true, |
| 94 | + Validators: []validator.String{ |
| 95 | + stringvalidator.RegexMatches(regexp.MustCompile(`^socks5:\/\/.*:\d+$`), |
| 96 | + "`proxy` must have the format of `socks5://<ip>:<port>`"), |
| 97 | + }, |
| 98 | + }, |
| 99 | + "private_ip": schema.BoolAttribute{ |
| 100 | + MarkdownDescription: "Use the private IP address of the Cloud SQL Postgresql instance to connect to", |
| 101 | + Description: "Use the private IP address of the Cloud SQL Postgresql instance to connect to", |
| 102 | + Optional: true, |
| 103 | + }, |
| 104 | + "psc": schema.BoolAttribute{ |
| 105 | + MarkdownDescription: "Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to", |
| 106 | + Description: "Use the Private Service Connect endpoint of the Cloud SQL Postgresql instance to connect to", |
| 107 | + Optional: true, |
| 108 | + }, |
| 109 | + "ssl_mode": schema.StringAttribute{ |
| 110 | + MarkdownDescription: "Determine the security of the connection to the Cloud SQL Postgresql instance", |
| 111 | + Description: "Determine the security of the connection to the Cloud SQL Postgresql instance", |
| 112 | + Optional: true, |
| 113 | + Validators: []validator.String{ |
| 114 | + stringvalidator.RegexMatches(regexp.MustCompile(`^(disable|allow|prefer|require)$`), |
| 115 | + "`ssl_mode` must be a supported ssl mode. One of 'disable', 'allow', 'prefer' or 'require'"), // TODO: add support for verify-ca and verify-full |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + PlanModifiers: []planmodifier.Object{ |
| 120 | + objectplanmodifier.RequiresReplace(), |
| 121 | + }, |
| 122 | + } |
| 123 | +} |
0 commit comments