Skip to content

Commit

Permalink
Use reusable idp package.
Browse files Browse the repository at this point in the history
  • Loading branch information
korenyoni committed Feb 27, 2024
1 parent de02dd7 commit ed93fc1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 593 deletions.
92 changes: 41 additions & 51 deletions codefresh/resource_account_idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/idp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -51,16 +52,14 @@ func resourceAccountIdp() *schema.Resource {
}),
),
// Defined in resource_idp, as schema is the same for global and account scoped IDPs
Schema: idpSchema,
Schema: idp.IdpSchema,
}
}

func resourceAccountIDPCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfclient.Client)

id, err := client.CreateIDP(mapResourceToAccountIDP(d), false)

if err != nil {
log.Printf("[DEBUG] Error while creating idp. Error = %v", err)
return err
Expand All @@ -71,15 +70,13 @@ func resourceAccountIDPCreate(d *schema.ResourceData, meta interface{}) error {
}

func resourceAccountIDPRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfclient.Client)
idpID := d.Id()

var cfClientIDP *cfclient.IDP
var err error

cfClientIDP, err = client.GetAccountIdpByID(idpID)

if err != nil {
if err.Error() == fmt.Sprintf("[ERROR] IDP with ID %s isn't found.", d.Id()) {
d.SetId("")
Expand All @@ -91,7 +88,6 @@ func resourceAccountIDPRead(d *schema.ResourceData, meta interface{}) error {
}

err = mapAccountIDPToResource(*cfClientIDP, d)

if err != nil {
log.Printf("[DEBUG] Error while getting mapping response to IDP object. Error = %v", err)
return err
Expand All @@ -104,7 +100,6 @@ func resourceAccountIDPDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)

err := client.DeleteIDPAccount(d.Id())

if err != nil {
log.Printf("[DEBUG] Error while deleting account level IDP. Error = %v", err)
return err
Expand All @@ -114,11 +109,9 @@ func resourceAccountIDPDelete(d *schema.ResourceData, meta interface{}) error {
}

func resourceAccountIDPUpdate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfclient.Client)

err := client.UpdateIDP(mapResourceToAccountIDP(d), false)

if err != nil {
log.Printf("[DEBUG] Error while updating idp. Error = %v", err)
return err
Expand All @@ -136,7 +129,7 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
d.Set("login_url", cfClientIDP.LoginUrl)
d.Set("client_type", cfClientIDP.ClientType)

if cfClientIDP.ClientType == "github" {
if cfClientIDP.ClientType == idp.GitHub {
attributes := []map[string]interface{}{{
"client_id": cfClientIDP.ClientId,
// Codefresh API Returns the client secret as an encrypted string on the server side
Expand All @@ -150,10 +143,10 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
"api_path_prefix": cfClientIDP.ApiPathPrefix,
}}

d.Set("github", attributes)
d.Set(idp.GitHub, attributes)
}

if cfClientIDP.ClientType == "gitlab" {
if cfClientIDP.ClientType == idp.GitLab {
attributes := []map[string]interface{}{{
"client_id": cfClientIDP.ClientId,
"client_secret": d.Get("gitlab.0.client_secret"),
Expand All @@ -162,10 +155,10 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
"api_url": cfClientIDP.ApiURL,
}}

d.Set("gitlab", attributes)
d.Set(idp.GitLab, attributes)
}

if cfClientIDP.ClientType == "okta" {
if cfClientIDP.ClientType == idp.Okta {
attributes := []map[string]interface{}{{
"client_id": cfClientIDP.ClientId,
"client_secret": d.Get("okta.0.client_secret"),
Expand All @@ -178,7 +171,7 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
d.Set("okta", attributes)
}

if cfClientIDP.ClientType == "google" {
if cfClientIDP.ClientType == idp.Google {
attributes := []map[string]interface{}{{
"client_id": cfClientIDP.ClientId,
"client_secret": d.Get("google.0.client_secret"),
Expand All @@ -188,23 +181,22 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
"sync_field": cfClientIDP.SyncField,
}}

d.Set("google", attributes)
d.Set(idp.Google, attributes)
}

if cfClientIDP.ClientType == "auth0" {
if cfClientIDP.ClientType == idp.Auth0 {
attributes := []map[string]interface{}{{
"client_id": cfClientIDP.ClientId,
"client_secret": d.Get("auth0.0.client_secret"),
"domain": cfClientIDP.ClientHost,
}}

d.Set("auth0", attributes)
d.Set(idp.Auth0, attributes)
}

if cfClientIDP.ClientType == "azure" {
if cfClientIDP.ClientType == idp.Azure {

syncInterval, err := strconv.Atoi(cfClientIDP.SyncInterval)

if err != nil {
return err
}
Expand All @@ -218,10 +210,10 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
"tenant": cfClientIDP.Tenant,
}}

d.Set("azure", attributes)
d.Set(idp.Azure, attributes)
}

if cfClientIDP.ClientType == "onelogin" {
if cfClientIDP.ClientType == idp.OneLogin {
attributes := []map[string]interface{}{{
"client_id": cfClientIDP.ClientId,
"client_secret": d.Get("onelogin.0.client_secret"),
Expand All @@ -234,23 +226,22 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
"app_id": cfClientIDP.AppId,
}}

d.Set("onelogin", attributes)
d.Set(idp.OneLogin, attributes)
}

if cfClientIDP.ClientType == "keycloak" {
if cfClientIDP.ClientType == idp.Keycloak {
attributes := []map[string]interface{}{{
"client_id": cfClientIDP.ClientId,
"client_secret": d.Get("keycloak.0.client_secret"),
"host": cfClientIDP.Host,
"realm": cfClientIDP.Realm,
}}

d.Set("keycloak", attributes)
d.Set(idp.Keycloak, attributes)
}

if cfClientIDP.ClientType == "saml" {
if cfClientIDP.ClientType == idp.SAML {
syncInterval, err := strconv.Atoi(cfClientIDP.SyncInterval)

if err != nil {
return err
}
Expand All @@ -269,10 +260,10 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
"access_token": d.Get("saml.0.access_token"),
}}

d.Set("saml", attributes)
d.Set(idp.SAML, attributes)
}

if cfClientIDP.ClientType == "ldap" {
if cfClientIDP.ClientType == idp.LDAP {
attributes := []map[string]interface{}{{
"url": cfClientIDP.Url,
"password": d.Get("ldap.0.password"),
Expand All @@ -284,14 +275,13 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
"search_base_for_sync": cfClientIDP.SearchBaseForSync,
}}

d.Set("ldap", attributes)
d.Set(idp.LDAP, attributes)
}

return nil
}

func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {

cfClientIDP := &cfclient.IDP{
ID: d.Id(),
DisplayName: d.Get("display_name").(string),
Expand All @@ -301,8 +291,8 @@ func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {
LoginUrl: d.Get("login_url").(string),
}

if _, ok := d.GetOk("github"); ok {
cfClientIDP.ClientType = "github"
if _, ok := d.GetOk(idp.GitHub); ok {
cfClientIDP.ClientType = idp.GitHub
cfClientIDP.ClientId = d.Get("github.0.client_id").(string)
cfClientIDP.ClientSecret = d.Get("github.0.client_secret").(string)
cfClientIDP.AuthURL = d.Get("github.0.authentication_url").(string)
Expand All @@ -312,17 +302,17 @@ func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {
cfClientIDP.ApiPathPrefix = d.Get("github.0.api_path_prefix").(string)
}

if _, ok := d.GetOk("gitlab"); ok {
cfClientIDP.ClientType = "gitlab"
if _, ok := d.GetOk(idp.GitLab); ok {
cfClientIDP.ClientType = idp.GitLab
cfClientIDP.ClientId = d.Get("gitlab.0.client_id").(string)
cfClientIDP.ClientSecret = d.Get("gitlab.0.client_secret").(string)
cfClientIDP.AuthURL = d.Get("gitlab.0.authentication_url").(string)
cfClientIDP.UserProfileURL = d.Get("gitlab.0.user_profile_url").(string)
cfClientIDP.ApiURL = d.Get("gitlab.0.api_url").(string)
}

if _, ok := d.GetOk("okta"); ok {
cfClientIDP.ClientType = "okta"
if _, ok := d.GetOk(idp.Okta); ok {
cfClientIDP.ClientType = idp.Okta
cfClientIDP.ClientId = d.Get("okta.0.client_id").(string)
cfClientIDP.ClientSecret = d.Get("okta.0.client_secret").(string)
cfClientIDP.ClientHost = d.Get("okta.0.client_host").(string)
Expand All @@ -331,8 +321,8 @@ func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {
cfClientIDP.Access_token = d.Get("okta.0.access_token").(string)
}

if _, ok := d.GetOk("google"); ok {
cfClientIDP.ClientType = "google"
if _, ok := d.GetOk(idp.Google); ok {
cfClientIDP.ClientType = idp.Google
cfClientIDP.ClientId = d.Get("google.0.client_id").(string)
cfClientIDP.ClientSecret = d.Get("google.0.client_secret").(string)
cfClientIDP.KeyFile = d.Get("google.0.json_keyfile").(string)
Expand All @@ -341,15 +331,15 @@ func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {
cfClientIDP.SyncField = d.Get("google.0.sync_field").(string)
}

if _, ok := d.GetOk("auth0"); ok {
cfClientIDP.ClientType = "auth0"
if _, ok := d.GetOk(idp.Auth0); ok {
cfClientIDP.ClientType = idp.Auth0
cfClientIDP.ClientId = d.Get("auth0.0.client_id").(string)
cfClientIDP.ClientSecret = d.Get("auth0.0.client_secret").(string)
cfClientIDP.ClientHost = d.Get("auth0.0.domain").(string)
}

if _, ok := d.GetOk("azure"); ok {
cfClientIDP.ClientType = "azure"
if _, ok := d.GetOk(idp.Azure); ok {
cfClientIDP.ClientType = idp.Azure
cfClientIDP.ClientId = d.Get("azure.0.app_id").(string)
cfClientIDP.ClientSecret = d.Get("azure.0.client_secret").(string)
cfClientIDP.AppId = d.Get("azure.0.object_id").(string)
Expand All @@ -358,8 +348,8 @@ func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {
cfClientIDP.SyncInterval = strconv.Itoa(d.Get("azure.0.sync_interval").(int))
}

if _, ok := d.GetOk("onelogin"); ok {
cfClientIDP.ClientType = "onelogin"
if _, ok := d.GetOk(idp.OneLogin); ok {
cfClientIDP.ClientType = idp.OneLogin
cfClientIDP.ClientId = d.Get("onelogin.0.client_id").(string)
cfClientIDP.ClientSecret = d.Get("onelogin.0.client_secret").(string)
cfClientIDP.ClientHost = d.Get("onelogin.0.domain").(string)
Expand All @@ -368,16 +358,16 @@ func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {
cfClientIDP.ApiClientSecret = d.Get("onelogin.0.api_client_secret").(string)
}

if _, ok := d.GetOk("keycloak"); ok {
cfClientIDP.ClientType = "keycloak"
if _, ok := d.GetOk(idp.Keycloak); ok {
cfClientIDP.ClientType = idp.Keycloak
cfClientIDP.ClientId = d.Get("keycloak.0.client_id").(string)
cfClientIDP.ClientSecret = d.Get("keycloak.0.client_secret").(string)
cfClientIDP.Host = d.Get("keycloak.0.host").(string)
cfClientIDP.Realm = d.Get("keycloak.0.realm").(string)
}

if _, ok := d.GetOk("saml"); ok {
cfClientIDP.ClientType = "saml"
if _, ok := d.GetOk(idp.SAML); ok {
cfClientIDP.ClientType = idp.SAML
cfClientIDP.SamlProvider = d.Get("saml.0.provider").(string)
cfClientIDP.EntryPoint = d.Get("saml.0.endpoint").(string)
cfClientIDP.ApplicationCert = d.Get("saml.0.application_certificate").(string)
Expand All @@ -392,8 +382,8 @@ func mapResourceToAccountIDP(d *schema.ResourceData) *cfclient.IDP {
cfClientIDP.Access_token = d.Get("saml.0.access_token").(string)
}

if _, ok := d.GetOk("ldap"); ok {
cfClientIDP.ClientType = "ldap"
if _, ok := d.GetOk(idp.LDAP); ok {
cfClientIDP.ClientType = idp.LDAP
cfClientIDP.Url = d.Get("ldap.0.url").(string)
cfClientIDP.Password = d.Get("ldap.0.password").(string)
cfClientIDP.DistinguishedName = d.Get("ldap.0.distinguished_name").(string)
Expand Down
Loading

0 comments on commit ed93fc1

Please sign in to comment.