|
| 1 | +package tco |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + organizationv20210331 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 10 | + |
| 11 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 12 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 13 | +) |
| 14 | + |
| 15 | +func ResourceTencentCloudOrganizationExternalSamlIdentityProvider() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceTencentCloudOrganizationExternalSamlIdentityProviderCreate, |
| 18 | + Read: resourceTencentCloudOrganizationExternalSamlIdentityProviderRead, |
| 19 | + Delete: resourceTencentCloudOrganizationExternalSamlIdentityProviderDelete, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "zone_id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + ForceNew: true, |
| 25 | + Description: "Space ID.", |
| 26 | + }, |
| 27 | + |
| 28 | + "encoded_metadata_document": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Optional: true, |
| 31 | + ForceNew: true, |
| 32 | + Description: "IdP metadata document (Base64 encoded). Provided by an IdP that supports the SAML 2.0 protocol.", |
| 33 | + }, |
| 34 | + |
| 35 | + "sso_status": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + ForceNew: true, |
| 39 | + Description: "SSO enabling status. Valid values: Enabled, Disabled (default).", |
| 40 | + }, |
| 41 | + |
| 42 | + "entity_id": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + ForceNew: true, |
| 46 | + Description: "IdP identifier.", |
| 47 | + }, |
| 48 | + |
| 49 | + "login_url": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + ForceNew: true, |
| 53 | + Description: "IdP login URL.", |
| 54 | + }, |
| 55 | + |
| 56 | + "x509_certificate": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + ForceNew: true, |
| 60 | + Description: "X509 certificate in PEM format. If this parameter is specified, all existing certificates will be replaced.", |
| 61 | + }, |
| 62 | + |
| 63 | + // computed |
| 64 | + "create_time": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: "Create time.", |
| 68 | + }, |
| 69 | + |
| 70 | + "update_time": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "Update time.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func resourceTencentCloudOrganizationExternalSamlIdentityProviderCreate(d *schema.ResourceData, meta interface{}) error { |
| 80 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_external_saml_identity_provider.create")() |
| 81 | + defer tccommon.InconsistentCheck(d, meta)() |
| 82 | + |
| 83 | + var ( |
| 84 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 85 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 86 | + request = organizationv20210331.NewSetExternalSAMLIdentityProviderRequest() |
| 87 | + zoneId string |
| 88 | + ) |
| 89 | + |
| 90 | + if v, ok := d.GetOk("zone_id"); ok { |
| 91 | + request.ZoneId = helper.String(v.(string)) |
| 92 | + zoneId = v.(string) |
| 93 | + } |
| 94 | + |
| 95 | + if v, ok := d.GetOk("encoded_metadata_document"); ok { |
| 96 | + request.EncodedMetadataDocument = helper.String(v.(string)) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("sso_status"); ok { |
| 100 | + request.SSOStatus = helper.String(v.(string)) |
| 101 | + } |
| 102 | + |
| 103 | + if v, ok := d.GetOk("entity_id"); ok { |
| 104 | + request.EntityId = helper.String(v.(string)) |
| 105 | + } |
| 106 | + |
| 107 | + if v, ok := d.GetOk("login_url"); ok { |
| 108 | + request.LoginUrl = helper.String(v.(string)) |
| 109 | + } |
| 110 | + |
| 111 | + if v, ok := d.GetOk("x509_certificate"); ok { |
| 112 | + request.X509Certificate = helper.String(v.(string)) |
| 113 | + } |
| 114 | + |
| 115 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 116 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().SetExternalSAMLIdentityProviderWithContext(ctx, request) |
| 117 | + if e != nil { |
| 118 | + return tccommon.RetryError(e) |
| 119 | + } else { |
| 120 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | + }) |
| 125 | + |
| 126 | + if reqErr != nil { |
| 127 | + log.Printf("[CRITAL]%s create organization external saml identity provider failed, reason:%+v", logId, reqErr) |
| 128 | + return reqErr |
| 129 | + } |
| 130 | + |
| 131 | + d.SetId(zoneId) |
| 132 | + return resourceTencentCloudOrganizationExternalSamlIdentityProviderRead(d, meta) |
| 133 | +} |
| 134 | + |
| 135 | +func resourceTencentCloudOrganizationExternalSamlIdentityProviderRead(d *schema.ResourceData, meta interface{}) error { |
| 136 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_external_saml_identity_provider.read")() |
| 137 | + defer tccommon.InconsistentCheck(d, meta)() |
| 138 | + |
| 139 | + var ( |
| 140 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 141 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 142 | + service = OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 143 | + zoneId = d.Id() |
| 144 | + ) |
| 145 | + |
| 146 | + respData, err := service.DescribeOrganizationExternalSamlIdentityProviderById(ctx, zoneId) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + if respData == nil { |
| 152 | + log.Printf("[WARN]%s resource `tencentcloud_organization_external_saml_identity_provider` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 153 | + d.SetId("") |
| 154 | + return nil |
| 155 | + } |
| 156 | + |
| 157 | + _ = d.Set("zone_id", zoneId) |
| 158 | + |
| 159 | + if respData.EncodedMetadataDocument != nil { |
| 160 | + _ = d.Set("encoded_metadata_document", respData.EncodedMetadataDocument) |
| 161 | + } |
| 162 | + |
| 163 | + if respData.SSOStatus != nil { |
| 164 | + _ = d.Set("sso_status", respData.SSOStatus) |
| 165 | + } |
| 166 | + |
| 167 | + if respData.EntityId != nil { |
| 168 | + _ = d.Set("entity_id", respData.EntityId) |
| 169 | + } |
| 170 | + |
| 171 | + if respData.LoginUrl != nil { |
| 172 | + _ = d.Set("login_url", respData.LoginUrl) |
| 173 | + } |
| 174 | + |
| 175 | + if respData.CertificateIds != nil { |
| 176 | + _ = d.Set("certificate_ids", respData.CertificateIds) |
| 177 | + } |
| 178 | + |
| 179 | + if respData.CreateTime != nil { |
| 180 | + _ = d.Set("create_time", respData.CreateTime) |
| 181 | + } |
| 182 | + |
| 183 | + if respData.UpdateTime != nil { |
| 184 | + _ = d.Set("update_time", respData.UpdateTime) |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +func resourceTencentCloudOrganizationExternalSamlIdentityProviderDelete(d *schema.ResourceData, meta interface{}) error { |
| 191 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_external_saml_identity_provider.delete")() |
| 192 | + defer tccommon.InconsistentCheck(d, meta)() |
| 193 | + |
| 194 | + var ( |
| 195 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 196 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 197 | + request = organizationv20210331.NewClearExternalSAMLIdentityProviderRequest() |
| 198 | + zoneId = d.Id() |
| 199 | + ) |
| 200 | + |
| 201 | + request.ZoneId = &zoneId |
| 202 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 203 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().ClearExternalSAMLIdentityProviderWithContext(ctx, request) |
| 204 | + if e != nil { |
| 205 | + return tccommon.RetryError(e) |
| 206 | + } else { |
| 207 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 208 | + } |
| 209 | + |
| 210 | + return nil |
| 211 | + }) |
| 212 | + |
| 213 | + if reqErr != nil { |
| 214 | + log.Printf("[CRITAL]%s delete organization external saml identity provider failed, reason:%+v", logId, reqErr) |
| 215 | + return reqErr |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments