Skip to content

Instantly share code, notes, and snippets.

@tombuildsstuff
Created January 23, 2019 15:26
Show Gist options
  • Save tombuildsstuff/4abf54d3cf4d831dabcc77fc1c5b58ed to your computer and use it in GitHub Desktop.
Save tombuildsstuff/4abf54d3cf4d831dabcc77fc1c5b58ed to your computer and use it in GitHub Desktop.
package azuread
import (
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/hashicorp/terraform/helper/schema"
)
func dataDomains() *schema.Resource {
return &schema.Resource{
Read: dataSourceActiveDirectoryDomainsRead,
Schema: map[string]*schema.Schema{
"only_default": {
Type: schema.TypeBool,
Optional: true,
},
"only_tenant_domain": {
Type: schema.TypeBool,
Optional: true,
},
"only_verified": {
Type: schema.TypeBool,
Optional: true,
},
"domains": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain_name": {
Type: schema.TypeString,
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
},
"is_initial": {
Type: schema.TypeBool,
Computed: true,
},
"is_verified": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataSourceActiveDirectoryDomainsRead(d *schema.ResourceData, meta interface{}) error {
tenantId := meta.(*ArmClient).tenantID
client := meta.(*ArmClient).domainsClient
ctx := meta.(*ArmClient).StopContext
onlyDefault := d.Get("only_default").(bool)
onlyTenantDomain := d.Get("only_tenant_domain").(bool)
onlyVerified := d.Get("only_verified").(bool)
results, err := client.List(ctx, "")
if err != nil {
return fmt.Errorf("Error listing Azure AD Domains: %+v", err)
}
d.SetId("domains-" + tenantId)
domains := flattenDomains(results.Value, onlyDefault, onlyTenantDomain, onlyVerified)
if len(domains) == 0 {
return fmt.Errorf("Error: No domains were returned based on those filters")
}
if err = d.Set("domains", domains); err != nil {
return fmt.Errorf("Error setting `domains`: %+v", err)
}
return nil
}
func flattenDomains(input *[]graphrbac.Domain, onlyDefault, onlyTenantDomain, onlyVerified bool) []interface{} {
if input == nil {
return []interface{}{}
}
domains := make([]interface{}, 0)
for _, v := range *input {
if v.Name == nil {
log.Printf("[DEBUG] Domain Name was nil - skipping")
continue
}
domainName := *v.Name
isDefault := false
if v.IsDefault != nil {
isDefault = *v.IsDefault
}
isInitial := false
if v.AdditionalProperties["isInitial"] != nil {
isInitial = v.AdditionalProperties["isInitial"].(bool)
}
isVerified := false
if v.IsVerified != nil {
isVerified = *v.IsVerified
}
domain := map[string]interface{}{
"domain_name": domainName,
"is_default": isDefault,
"is_initial": isInitial,
"is_verified": isVerified,
}
// Filters
if onlyDefault && !isDefault {
log.Printf("[DEBUG] Skipping %q since the filter requires the default domain", domainName)
continue
}
if onlyTenantDomain && !isInitial {
log.Printf("[DEBUG] Skipping %q since the filter requires the tenant domain", domainName)
continue
}
if onlyVerified && !isVerified {
log.Printf("[DEBUG] Skipping %q since the filter requires the tenant domain", domainName)
continue
}
domains = append(domains, domain)
}
return domains
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment