|
| 1 | +/* |
| 2 | +Provides a resource to create a CAM-ROLE-SSO (Only support OIDC). |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cam_role_sso" "foo" { |
| 8 | + name="test" |
| 9 | + identity_url="https://login.microsoftonline.com/.../v2.0" |
| 10 | + identity_key="..." |
| 11 | + client_ids=["..."] |
| 12 | + description="this is a description" |
| 13 | +} |
| 14 | +``` |
| 15 | +
|
| 16 | +Import |
| 17 | +
|
| 18 | +CAM-ROLE-SSO can be imported using the `name`, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +$ terraform import tencentcloud_cam_role_sso.foo "test" |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "fmt" |
| 28 | + "log" |
| 29 | + |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 32 | + cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" |
| 33 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudCamRoleSSO() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudCamRoleSSOCreate, |
| 39 | + Read: resourceTencentCloudCamRoleSSORead, |
| 40 | + Update: resourceTencentCloudCamRoleSSOUpdate, |
| 41 | + Delete: resourceTencentCloudCamRoleSSODelete, |
| 42 | + Importer: &schema.ResourceImporter{ |
| 43 | + State: schema.ImportStatePassthrough, |
| 44 | + }, |
| 45 | + |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Required: true, |
| 50 | + Description: "The name of resource.", |
| 51 | + }, |
| 52 | + "identity_url": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Required: true, |
| 55 | + Description: "Identity provider URL.", |
| 56 | + }, |
| 57 | + "identity_key": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + Description: "Sign the public key.", |
| 61 | + }, |
| 62 | + "client_ids": { |
| 63 | + Type: schema.TypeSet, |
| 64 | + Required: true, |
| 65 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 66 | + Description: "Client ids.", |
| 67 | + }, |
| 68 | + "description": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Optional: true, |
| 71 | + Description: "The description of resource.", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func resourceTencentCloudCamRoleSSOCreate(d *schema.ResourceData, meta interface{}) error { |
| 78 | + defer logElapsed("resource.tencentcloud_cam_role_sso.create")() |
| 79 | + |
| 80 | + logId := getLogId(contextNil) |
| 81 | + |
| 82 | + request := cam.NewCreateOIDCConfigRequest() |
| 83 | + request.Name = helper.String(d.Get("name").(string)) |
| 84 | + request.IdentityUrl = helper.String(d.Get("identity_url").(string)) |
| 85 | + request.IdentityKey = helper.String(d.Get("identity_key").(string)) |
| 86 | + request.Description = helper.String(d.Get("description").(string)) |
| 87 | + request.ClientId = helper.InterfacesStringsPoint(d.Get("client_ids").(*schema.Set).List()) |
| 88 | + |
| 89 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 90 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().CreateOIDCConfig(request) |
| 91 | + if e != nil { |
| 92 | + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", |
| 93 | + logId, request.GetAction(), request.ToJsonString(), e.Error()) |
| 94 | + return retryError(e) |
| 95 | + } else { |
| 96 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 97 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 98 | + } |
| 99 | + return nil |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + log.Printf("[CRITAL]%s create CAM role SSO failed, reason:%s\n", logId, err.Error()) |
| 103 | + return err |
| 104 | + } |
| 105 | + d.SetId(d.Get("name").(string)) |
| 106 | + return resourceTencentCloudCamRoleSSORead(d, meta) |
| 107 | +} |
| 108 | + |
| 109 | +func resourceTencentCloudCamRoleSSORead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + defer logElapsed("resource.tencentcloud_cam_role_sso.read")() |
| 111 | + |
| 112 | + logId := getLogId(contextNil) |
| 113 | + request := cam.NewDescribeOIDCConfigRequest() |
| 114 | + request.Name = helper.String(d.Id()) |
| 115 | + var response *cam.DescribeOIDCConfigResponse |
| 116 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 117 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().DescribeOIDCConfig(request) |
| 118 | + if e != nil { |
| 119 | + return retryError(e) |
| 120 | + } |
| 121 | + response = result |
| 122 | + return nil |
| 123 | + }) |
| 124 | + if err != nil { |
| 125 | + log.Printf("[CRITAL]%s read CAM role SSO failed, reason:%s\n", logId, err.Error()) |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + if response.Response == nil { |
| 130 | + d.SetId("") |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + _ = d.Set("identity_key", *response.Response.IdentityKey) |
| 135 | + _ = d.Set("identity_url", *response.Response.IdentityUrl) |
| 136 | + _ = d.Set("name", *response.Response.Name) |
| 137 | + _ = d.Set("description", *response.Response.Description) |
| 138 | + clientIds := make([]string, 0) |
| 139 | + for _, clientId := range response.Response.ClientId { |
| 140 | + clientIds = append(clientIds, *clientId) |
| 141 | + } |
| 142 | + _ = d.Set("client_ids", clientIds) |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func resourceTencentCloudCamRoleSSOUpdate(d *schema.ResourceData, meta interface{}) error { |
| 148 | + defer logElapsed("resource.tencentcloud_cam_role_sso.update")() |
| 149 | + logId := getLogId(contextNil) |
| 150 | + request := cam.NewUpdateOIDCConfigRequest() |
| 151 | + if d.HasChange("name") { |
| 152 | + return fmt.Errorf("not support change name") |
| 153 | + } |
| 154 | + request.Name = helper.String(d.Id()) |
| 155 | + if d.HasChange("identity_url") || d.HasChange("identity_key") || d.HasChange("description") || d.HasChange("client_ids") { |
| 156 | + request.IdentityKey = helper.String(d.Get("identity_key").(string)) |
| 157 | + request.IdentityUrl = helper.String(d.Get("identity_url").(string)) |
| 158 | + request.Description = helper.String(d.Get("description").(string)) |
| 159 | + request.ClientId = helper.InterfacesStringsPoint(d.Get("client_ids").(*schema.Set).List()) |
| 160 | + } |
| 161 | + |
| 162 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 163 | + _, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().UpdateOIDCConfig(request) |
| 164 | + if e != nil { |
| 165 | + return retryError(e) |
| 166 | + } |
| 167 | + return nil |
| 168 | + }) |
| 169 | + if err != nil { |
| 170 | + log.Printf("[CRITAL]%s update CAM Role SSO failed, reason:%s\n", logId, err.Error()) |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + return resourceTencentCloudCamRoleSSORead(d, meta) |
| 175 | +} |
| 176 | + |
| 177 | +func resourceTencentCloudCamRoleSSODelete(d *schema.ResourceData, meta interface{}) error { |
| 178 | + defer logElapsed("resource.tencentcloud_cam_role_sso.delete")() |
| 179 | + logId := getLogId(contextNil) |
| 180 | + request := cam.NewDeleteOIDCConfigRequest() |
| 181 | + name := d.Id() |
| 182 | + request.Name = helper.String(name) |
| 183 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 184 | + _, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().DeleteOIDCConfig(request) |
| 185 | + if e != nil { |
| 186 | + return retryError(e) |
| 187 | + } |
| 188 | + return nil |
| 189 | + }) |
| 190 | + if err != nil { |
| 191 | + log.Printf("[CRITAL]%s disable cam sso failed, reason:%s\n", logId, err.Error()) |
| 192 | + return err |
| 193 | + } |
| 194 | + return nil |
| 195 | +} |
0 commit comments