-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from pingidentity/pingone_resource_scope
pingone_resource_scope resource export
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
internal/connector/pingone/resources/sso/pingone_resource_scope.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package sso | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pingidentity/pingctl/internal/connector" | ||
"github.com/pingidentity/pingctl/internal/connector/pingone/resources/common" | ||
"github.com/pingidentity/pingctl/internal/logger" | ||
) | ||
|
||
// Verify that the resource satisfies the exportable resource interface | ||
var ( | ||
_ connector.ExportableResource = &PingoneResourceScopeResource{} | ||
) | ||
|
||
type PingoneResourceScopeResource struct { | ||
clientInfo *connector.SDKClientInfo | ||
} | ||
|
||
// Utility method for creating a PingoneResourceScopeResource | ||
func ResourceScope(clientInfo *connector.SDKClientInfo) *PingoneResourceScopeResource { | ||
return &PingoneResourceScopeResource{ | ||
clientInfo: clientInfo, | ||
} | ||
} | ||
|
||
func (r *PingoneResourceScopeResource) ExportAll() (*[]connector.ImportBlock, error) { | ||
l := logger.Get() | ||
|
||
l.Debug().Msgf("Fetching all %s resources...", r.ResourceType()) | ||
|
||
apiExecuteFunc := r.clientInfo.ApiClient.ManagementAPIClient.ResourcesApi.ReadAllResources(r.clientInfo.Context, r.clientInfo.ExportEnvironmentID).Execute | ||
apiFunctionName := "ReadAllResources" | ||
|
||
embedded, err := common.GetManagementEmbedded(apiExecuteFunc, apiFunctionName, r.ResourceType()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
importBlocks := []connector.ImportBlock{} | ||
|
||
l.Debug().Msgf("Generating Import Blocks for all %s resources...", r.ResourceType()) | ||
|
||
for _, resource := range embedded.GetResources() { | ||
resourceId, resourceIdOk := resource.GetIdOk() | ||
resourceName, resourceNameOk := resource.GetNameOk() | ||
|
||
if resourceIdOk && resourceNameOk { | ||
apiResourceScopesExecuteFunc := r.clientInfo.ApiClient.ManagementAPIClient.ResourceScopesApi.ReadAllResourceScopes(r.clientInfo.Context, r.clientInfo.ExportEnvironmentID, *resourceId).Execute | ||
apiResourceScopesFunctionName := "ReadAllResourceScopes" | ||
|
||
embeddedResourceScopes, err := common.GetManagementEmbedded(apiResourceScopesExecuteFunc, apiResourceScopesFunctionName, r.ResourceType()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, scope := range embeddedResourceScopes.GetScopes() { | ||
scopeId, scopeIdOk := scope.GetIdOk() | ||
scopeName, scopeNameOk := scope.GetNameOk() | ||
if scopeIdOk && scopeNameOk { | ||
importBlocks = append(importBlocks, connector.ImportBlock{ | ||
ResourceType: r.ResourceType(), | ||
ResourceName: fmt.Sprintf("%s_%s", *resourceName, *scopeName), | ||
ResourceID: fmt.Sprintf("%s/%s/%s", r.clientInfo.ExportEnvironmentID, *resourceId, *scopeId), | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return &importBlocks, nil | ||
} | ||
|
||
func (r *PingoneResourceScopeResource) ResourceType() string { | ||
return "pingone_resource_scope" | ||
} |