-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cdb): [117461373]support mysql ssl #2687
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
tencentcloud_mysql_ssl | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package cdb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" | ||
|
||
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" | ||
) | ||
|
||
func ResourceTencentCloudMysqlSsl() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceTencentCloudMysqlSslCreate, | ||
Read: resourceTencentCloudMysqlSslRead, | ||
Update: resourceTencentCloudMysqlSslUpdate, | ||
Delete: resourceTencentCloudMysqlSslDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"instance_id": { | ||
Required: true, | ||
Type: schema.TypeString, | ||
Description: "Instance ID. Example value: cdb-c1nl9rpv.", | ||
}, | ||
|
||
"status": { | ||
Required: true, | ||
Type: schema.TypeString, | ||
Description: "Whether to enable SSL. `ON` means enabled, `OFF` means not enabled.", | ||
}, | ||
|
||
"url": { | ||
Computed: true, | ||
Type: schema.TypeString, | ||
Description: "The certificate download link. Example value: http://testdownload.url.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTencentCloudMysqlSslCreate(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_mysql_ssl.create")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
d.SetId(d.Get("instance_id").(string)) | ||
|
||
return resourceTencentCloudMysqlSslUpdate(d, meta) | ||
} | ||
|
||
func resourceTencentCloudMysqlSslRead(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_mysql_ssl.read")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
logId := tccommon.GetLogId(tccommon.ContextNil) | ||
|
||
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) | ||
|
||
service := MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} | ||
|
||
instanceId := d.Id() | ||
|
||
ssl, err := service.DescribeMysqlSslById(ctx, instanceId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if ssl == nil { | ||
d.SetId("") | ||
log.Printf("[WARN]%s resource `tencentcloud_mysql_ssl` [%s] not found, please check if it has been deleted.", | ||
logId, instanceId, | ||
) | ||
return nil | ||
} | ||
|
||
_ = d.Set("instance_id", instanceId) | ||
|
||
if ssl.Status != nil { | ||
_ = d.Set("status", ssl.Status) | ||
} | ||
|
||
if ssl.Url != nil { | ||
_ = d.Set("url", ssl.Url) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceTencentCloudMysqlSslUpdate(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_mysql_ssl.update")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
logId := tccommon.GetLogId(tccommon.ContextNil) | ||
|
||
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) | ||
|
||
instanceId := d.Id() | ||
|
||
status := "" | ||
if v, ok := d.GetOk("status"); ok { | ||
status = v.(string) | ||
if status == "ON" { | ||
request := mysql.NewOpenSSLRequest() | ||
request.InstanceId = helper.String(instanceId) | ||
|
||
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { | ||
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMysqlClient().OpenSSL(request) | ||
if e != nil { | ||
return tccommon.RetryError(e) | ||
} else { | ||
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
log.Printf("[CRITAL]%s update mysql ssl failed, reason:%+v", logId, err) | ||
return err | ||
} | ||
} else if status == "OFF" { | ||
request := mysql.NewCloseSSLRequest() | ||
request.InstanceId = helper.String(instanceId) | ||
|
||
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { | ||
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMysqlClient().CloseSSL(request) | ||
if e != nil { | ||
return tccommon.RetryError(e) | ||
} else { | ||
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
log.Printf("[CRITAL]%s update mysql ssl failed, reason:%+v", logId, err) | ||
return err | ||
} | ||
} else { | ||
return fmt.Errorf("[CRITAL]%s update mysql ssl failed, reason:your status must be ON or OFF!", logId) | ||
} | ||
|
||
if status != "" { | ||
service := MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} | ||
err := resource.Retry(7*tccommon.ReadRetryTimeout, func() *resource.RetryError { | ||
ssl, err := service.DescribeMysqlSslById(ctx, instanceId) | ||
if err != nil { | ||
return resource.NonRetryableError(err) | ||
} | ||
if ssl == nil { | ||
err = fmt.Errorf("mysqlid %s instance ssl not exists", instanceId) | ||
return resource.NonRetryableError(err) | ||
} | ||
if *ssl.Status != status { | ||
return resource.RetryableError(fmt.Errorf("mysql ssl status is (%v)", *ssl.Status)) | ||
} | ||
if *ssl.Status == status { | ||
return nil | ||
} | ||
err = fmt.Errorf("mysql ssl status is %v,we won't wait for it finish", *ssl.Status) | ||
return resource.NonRetryableError(err) | ||
}) | ||
|
||
if err != nil { | ||
log.Printf("[CRITAL]%s mysql switchForUpgrade fail, reason:%s\n ", logId, err.Error()) | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return resourceTencentCloudMysqlSslRead(d, meta) | ||
} | ||
|
||
func resourceTencentCloudMysqlSslDelete(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_mysql_ssl.delete")() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Provides a resource to create a mysql ssl | ||
|
||
Example Usage | ||
|
||
```hcl | ||
resource "tencentcloud_mysql_ssl" "ssl" { | ||
instance_id = "cdb-j5rprr8n" | ||
status = "OFF" | ||
} | ||
``` | ||
|
||
Import | ||
|
||
mysql ssl can be imported using the id, e.g. | ||
|
||
``` | ||
terraform import tencentcloud_mysql_ssl.ssl instanceId | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cdb_test | ||
|
||
import ( | ||
"testing" | ||
|
||
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccTencentCloudMysqlSslResource_basic(t *testing.T) { | ||
t.Parallel() | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
tcacctest.AccPreCheck(t) | ||
}, | ||
Providers: tcacctest.AccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMysqlSsl, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("tencentcloud_mysql_ssl.ssl", "id"), | ||
resource.TestCheckResourceAttr("tencentcloud_mysql_ssl.ssl", "status", "ON"), | ||
resource.TestCheckResourceAttrSet("tencentcloud_mysql_ssl.ssl", "url"), | ||
), | ||
}, | ||
{ | ||
ResourceName: "tencentcloud_mysql_ssl.ssl", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: testAccMysqlSslUp, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("tencentcloud_mysql_ssl.ssl", "id"), | ||
resource.TestCheckResourceAttr("tencentcloud_mysql_ssl.ssl", "status", "OFF"), | ||
resource.TestCheckResourceAttrSet("tencentcloud_mysql_ssl.ssl", "url"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccMysqlSsl = testAccMysql + ` | ||
|
||
resource "tencentcloud_mysql_ssl" "ssl" { | ||
instance_id = tencentcloud_mysql_instance.mysql.id | ||
status = "ON" | ||
} | ||
|
||
` | ||
|
||
const testAccMysqlSslUp = testAccMysql + ` | ||
|
||
resource "tencentcloud_mysql_ssl" "ssl" { | ||
instance_id = tencentcloud_mysql_instance.mysql.id | ||
status = "OFF" | ||
} | ||
|
||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里改成 True和 False,对于用户来说使用更简单。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
接口status返回就是ON和OFF,是不是这样更好一些