diff --git a/datadog/resource_datadog_organization_settings.go b/datadog/resource_datadog_organization_settings.go index 5a5a08f220..2d9b0750d0 100644 --- a/datadog/resource_datadog_organization_settings.go +++ b/datadog/resource_datadog_organization_settings.go @@ -1,8 +1,10 @@ package datadog import ( + "bytes" "context" "fmt" + "io" "time" "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" @@ -48,6 +50,21 @@ func resourceDatadogOrganizationSettings() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "saml_configurations": { + Description: "SAML Configurations", + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "idp_metadata": { + Type: schema.TypeString, + Required: true, + Description: "The content of metadata XML file.", + }, + }, + }, + }, "security_contacts": { Type: schema.TypeList, Optional: true, @@ -271,6 +288,26 @@ func buildDatadogOrganizationUpdateV1Struct(d *schema.ResourceData) *datadogV1.O return org } +func buildSamlConfigurationsStruct(d *schema.ResourceData) *datadogV2.SamlConfigurations { + samlConfigurations := datadogV2.NewSamlConfigurations() + // SAML configurations + if v, ok := d.GetOk("saml_configurations"); ok { + if samlConfigurationsSetList := v.([]interface{}); len(samlConfigurationsSetList) > 0 { + samlConfigurationsSet := samlConfigurationsSetList[0].(map[string]interface{}) + + // idp_metadata + if v, ok := samlConfigurationsSet["idp_metadata"]; ok { + fileContent := v.(string) + optionalParams := datadogV2.NewUploadIdPMetadataOptionalParameters() + var fileReader io.Reader = bytes.NewReader([]byte(fileContent)) + optionalParams.IdpFile = &fileReader + samlConfigurations.SetIdpMetadata(optionalParams) + } + } + } + return samlConfigurations +} + func resourceDatadogOrganizationSettingsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { // note: we don't actually create a new organization, we just import the org associated with the current API/APP keys providerConf := meta.(*ProviderConfiguration) @@ -318,6 +355,11 @@ func resourceDatadogOrganizationSettingsUpdate(ctx context.Context, d *schema.Re apiInstances := providerConf.DatadogApiInstances auth := providerConf.Auth + samlResp, err := apiInstances.GetOrganizationsApiV2().UploadIdPMetadata(auth, *buildSamlConfigurationsStruct(d).IdpMetadata) + if err != nil { + return utils.TranslateClientErrorDiag(err, samlResp, "error uploading saml") + } + resp, httpResponse, err := apiInstances.GetOrganizationsApiV1().UpdateOrg(auth, d.Id(), *buildDatadogOrganizationUpdateV1Struct(d)) if err != nil { return utils.TranslateClientErrorDiag(err, httpResponse, "error updating organization") diff --git a/examples/resources/datadog_organization_settings/resource.tf b/examples/resources/datadog_organization_settings/resource.tf index 6b591189cc..9fbafc9f3d 100644 --- a/examples/resources/datadog_organization_settings/resource.tf +++ b/examples/resources/datadog_organization_settings/resource.tf @@ -1,4 +1,7 @@ # Manage Datadog Organization resource "datadog_organization_settings" "organization" { name = "foo-organization" + saml_configurations { + idp_metadata= file("/path/to/metadata.xml") + } }