Skip to content

Commit

Permalink
Promote Redis Auth to GA (#4314) (#2819)
Browse files Browse the repository at this point in the history
Co-authored-by: upodroid <cy@borg.dev>
Signed-off-by: Modular Magician <magic-modules@google.com>

Co-authored-by: upodroid <cy@borg.dev>
  • Loading branch information
modular-magician and upodroid authored Dec 28, 2020
1 parent ed61df8 commit 8f07afa
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changelog/4314.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
redis: promoted `google_redis_instance.auth_enabled` to GA
```
```release-note:enhancement
redis: added `auth_string` output to `google_redis_instance` when `auth_enabled` is `true`
```
72 changes: 72 additions & 0 deletions google-beta/resource_redis_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ checked before each import/export operation.`,
Computed: true,
Description: `The port number of the exposed Redis endpoint.`,
},
"auth_string": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -342,6 +347,14 @@ func resourceRedisInstanceCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error waiting to create Instance: %s", err)
}

opRes, err = resourceRedisInstanceDecoder(d, meta, opRes)
if err != nil {
return fmt.Errorf("Error decoding response from operation: %s", err)
}
if opRes == nil {
return fmt.Errorf("Error decoding response from operation, could not find object")
}

if err := d.Set("name", flattenRedisInstanceName(opRes["name"], d, config)); err != nil {
return err
}
Expand Down Expand Up @@ -388,6 +401,18 @@ func resourceRedisInstanceRead(d *schema.ResourceData, meta interface{}) error {
return handleNotFoundError(err, d, fmt.Sprintf("RedisInstance %q", d.Id()))
}

res, err = resourceRedisInstanceDecoder(d, meta, res)
if err != nil {
return err
}

if res == nil {
// Decoding the object has resulted in it being gone. It may be marked deleted
log.Printf("[DEBUG] Removing RedisInstance because it no longer exists.")
d.SetId("")
return nil
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading Instance: %s", err)
}
Expand Down Expand Up @@ -816,3 +841,50 @@ func resourceRedisInstanceEncoder(d *schema.ResourceData, meta interface{}, obj
}
return obj, nil
}

func resourceRedisInstanceDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
config := meta.(*Config)

userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return nil, err
}

if v, ok := res["authEnabled"].(bool); ok {
if v {
url, err := replaceVars(d, config, "{{RedisBasePath}}projects/{{project}}/locations/{{region}}/instances/{{name}}/authString")
if err != nil {
return nil, err
}

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return nil, fmt.Errorf("Error fetching project for Instance: %s", err)
}

billingProject = project

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return nil, fmt.Errorf("Error reading AuthString: %s", err)
}

if err := d.Set("auth_string", res["authString"]); err != nil {
return nil, fmt.Errorf("Error reading Instance: %s", err)
}
}
} else {
if err := d.Set("auth_string", ""); err != nil {
return nil, fmt.Errorf("Error reading Instance: %s", err)
}
}

return res, nil
}
3 changes: 2 additions & 1 deletion website/docs/r/redis_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ The following arguments are supported:
[locationId].

* `auth_enabled` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
(Optional)
Optional. Indicates whether OSS Redis AUTH is enabled for the
instance. If set to "true" AUTH is enabled on the instance.
Default value is "false" meaning AUTH is disabled.
Expand Down Expand Up @@ -227,6 +227,7 @@ The following arguments are supported:
* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

* `auth_string` - (Optional) AUTH String set on the instance. This field will only be populated if auth_enabled is true.

## Attributes Reference

Expand Down

0 comments on commit 8f07afa

Please sign in to comment.