-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom_provider): Add new Data Source for Custom Providers (#70)
* feat(custom_provider): Add new Data Source for Custom Providers - Fix the Persistance Issue of Roles on Registration * fix go versions
- Loading branch information
1 parent
185b001
commit 40c3cae
Showing
9 changed files
with
165 additions
and
2 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
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
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
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,36 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type customProvidersResponse struct { | ||
Status int `json:"status"` | ||
Data CustomProvider `json:"data"` | ||
} | ||
|
||
func (c *client) GetCustomProvider(providerName string) (*CustomProvider, error) { | ||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/providers-srv/custom/%s", c.HostUrl, providerName), nil) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var response customProvidersResponse | ||
err = json.Unmarshal(body, &response) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &response.Data, nil | ||
|
||
} |
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
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,73 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type customProviderDataSource struct { | ||
provider *cidaasProvider | ||
} | ||
|
||
var _ datasource.DataSource = (*customProviderDataSource)(nil) | ||
|
||
func NewCustomProviderDataSource() datasource.DataSource { | ||
return &customProviderDataSource{} | ||
} | ||
|
||
func (d *customProviderDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Allows reading custom login providers that are configured", | ||
Attributes: map[string]schema.Attribute{ | ||
"display_name": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"provider_name": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *customProviderDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_custom_provider" | ||
} | ||
|
||
func (d *customProviderDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
d.provider, resp.Diagnostics = toProvider(req.ProviderData) | ||
} | ||
|
||
func (d customProviderDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var providerName string | ||
|
||
var state CustomProvider | ||
|
||
diags := req.Config.GetAttribute(ctx, path.Root("provider_name"), &providerName) | ||
resp.Diagnostics.Append(diags...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
customProvider, err := d.provider.client.GetCustomProvider(providerName) | ||
|
||
if err != nil { | ||
resp.Diagnostics.AddError("Could not fetch custom provider", | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
state.ProviderName = types.StringValue(customProvider.ProviderName) | ||
state.DisplayName = types.StringValue(customProvider.DisplayName) | ||
|
||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} |
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
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
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