Skip to content

Commit

Permalink
Fix import of tenant associated with cloud sites
Browse files Browse the repository at this point in the history
  • Loading branch information
shrsr authored and lhercot committed Oct 15, 2021
1 parent 7165e58 commit 977e877
Showing 1 changed file with 130 additions and 16 deletions.
146 changes: 130 additions & 16 deletions mso/resource_mso_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,116 @@ func resourceMSOTenantImport(d *schema.ResourceData, m interface{}) ([]*schema.R
mapSite := make(map[string]interface{})
mapSite["site_id"] = models.StripQuotes(sitesCont.S("siteId").String())
mapSite["security_domains"] = sitesCont.S("securityDomains").Data().([]interface{})
awsCount, err := sitesCont.ArrayCount("awsAccount")
if err == nil {
if awsCount > 0 {
awsCont, err := sitesCont.ArrayElement(0, "awsAccount")
if err == nil {
mapSite["aws_account_id"] = models.StripQuotes(awsCont.S("accountId").String())
if awsCont.Exists("isTrusted") {
mapSite["is_aws_account_trusted"] = awsCont.S("isTrusted").Data().(bool)
}
mapSite["vendor"] = "aws"
accessKey := models.StripQuotes(awsCont.S("accessKeyId").String())
secretKey := models.StripQuotes(awsCont.S("secretKey").String())

if accessKey != "{}" {
mapSite["aws_access_key_id"] = accessKey
}

if secretKey != "{}" {
mapSite["aws_secret_key"] = secretKey
}

} else {
log.Printf("Unable to load AWS credentials")
}
} else {
mapSite["aws_account_id"] = ""
mapSite["aws_access_key_id"] = ""
mapSite["aws_secret_key"] = ""
mapSite["is_aws_account_trusted"] = false
}
} else {
log.Printf("Error occurred while loading AWS creds")
mapSite["aws_account_id"] = ""
mapSite["aws_access_key_id"] = ""
mapSite["aws_secret_key"] = ""
mapSite["is_aws_account_trusted"] = false

}

azureCount, err := sitesCont.ArrayCount("azureAccount")
if err == nil {
if azureCount > 0 {
azureCont, err := sitesCont.ArrayElement(0, "azureAccount")
if err == nil {
mapSite["vendor"] = "azure"
mapSite["azure_access_type"] = models.StripQuotes(azureCont.S("accessType").String())
mapSite["azure_subscription_id"] = models.StripQuotes(azureCont.S("cloudSubscription", "cloudSubscriptionId").String())
if mapSite["azure_access_type"] == "credentials" {
mapSite["azure_application_id"] = models.StripQuotes(azureCont.S("cloudSubscription", "cloudApplicationId").String())

applicationCount, err := azureCont.ArrayCount("cloudApplication")
if err == nil {
if applicationCount > 0 {
appCont, err := azureCont.ArrayElement(0, "cloudApplication")
if err == nil {
mapSite["azure_client_secret"] = models.StripQuotes(appCont.S("secretKey").String())
mapSite["azure_active_directory_id"] = models.StripQuotes(appCont.S("cloudActiveDirectoryId").String())
} else {
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
}
} else {
// Set to empty string
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
}
} else {
// Set to empty string
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
}
}

} else {
if sitesCont.Exists("cloudAccount") && sitesCont.S("cloudAccount").String() != "{}" {
mapSite["azure_access_type"] = "shared"
cldAcc := strings.Split(models.StripQuotes(sitesCont.S("cloudAccount").String()), "/")
accInfo := strings.Split(cldAcc[2], "-")

mapSite["vendor"] = accInfo[3]
mapSite["azure_shared_account_id"] = (accInfo[1])[1 : len(accInfo[1])-1]

} else {
mapSite["azure_access_type"] = ""

}
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
mapSite["azure_subscription_id"] = ""
mapSite["azure_application_id"] = ""
}

} else {
log.Printf("Error occurred while loading count for azureAccount.")
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
mapSite["azure_access_type"] = ""
mapSite["azure_subscription_id"] = ""
mapSite["azure_application_id"] = ""
mapSite["azure_shared_account_id"] = ""
}
} else {
log.Printf("Error ocurred while loading azure credentials")
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
mapSite["azure_access_type"] = ""
mapSite["azure_subscription_id"] = ""
mapSite["azure_application_id"] = ""
}

site_associations = append(site_associations, mapSite)
}
d.Set("site_associations", site_associations)
Expand Down Expand Up @@ -686,29 +796,26 @@ func resourceMSOTenantRead(d *schema.ResourceData, m interface{}) error {
mapSite["azure_active_directory_id"] = ""
}
}

} else {
log.Printf("Error ocurred while loading azure credentials")
if sitesCont.Exists("cloudAccount") && sitesCont.S("cloudAccount").String() != "{}" {
mapSite["azure_access_type"] = "shared"
cldAcc := strings.Split(models.StripQuotes(sitesCont.S("cloudAccount").String()), "/")
accInfo := strings.Split(cldAcc[2], "-")

mapSite["vendor"] = accInfo[3]
mapSite["azure_shared_account_id"] = (accInfo[1])[1 : len(accInfo[1])-1]

} else {
mapSite["azure_access_type"] = ""

}
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
mapSite["azure_access_type"] = ""
mapSite["azure_subscription_id"] = ""
mapSite["azure_application_id"] = ""

}
}
} else {
if sitesCont.Exists("cloudAccount") && sitesCont.S("cloudAccount").String() != "{}" {
mapSite["azure_access_type"] = "shared"
cldAcc := strings.Split(models.StripQuotes(sitesCont.S("cloudAccount").String()), "/")
accInfo := strings.Split(cldAcc[2], "-")

mapSite["vendor"] = accInfo[3]
mapSite["azure_shared_account_id"] = (accInfo[1])[1 : len(accInfo[1])-1]

mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
mapSite["azure_subscription_id"] = ""
mapSite["azure_application_id"] = ""
} else {
log.Printf("Error occurred while loading count for azureAccount.")
mapSite["azure_client_secret"] = ""
Expand All @@ -718,6 +825,13 @@ func resourceMSOTenantRead(d *schema.ResourceData, m interface{}) error {
mapSite["azure_application_id"] = ""
mapSite["azure_shared_account_id"] = ""
}
} else {
log.Printf("Error ocurred while loading azure credentials")
mapSite["azure_client_secret"] = ""
mapSite["azure_active_directory_id"] = ""
mapSite["azure_access_type"] = ""
mapSite["azure_subscription_id"] = ""
mapSite["azure_application_id"] = ""
}

site_associations = append(site_associations, mapSite)
Expand Down

0 comments on commit 977e877

Please sign in to comment.