Skip to content

Enable upload IdP metadata file via terraform #2652

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions datadog/resource_datadog_organization_settings.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package datadog

import (
"bytes"
"context"
"fmt"
"io"
"time"

"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Manage Datadog Organization
resource "datadog_organization_settings" "organization" {
name = "foo-organization"
saml_configurations {
idp_metadata= file("/path/to/metadata.xml")
}
}