Skip to content

Commit

Permalink
feat(cidaas_social_provider): add support to filter by name (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
EldoranDev authored Jul 27, 2023
1 parent a78565d commit c28cac2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Client interface {
UpsertHook(hook Hook) (*Hook, error)
DeleteHook(ID string) error

GetSocialProvider(name string) (*SocialProvider, error)
GetSocialProvider(providerName string, name string) (*SocialProvider, error)

GetConsentInstance(name string) (*ConsentInstance, error)

Expand Down
1 change: 1 addition & 0 deletions internal/client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type HookApiKeyDetails struct {
type SocialProvider struct {
Id string `json:"id,omitempty"`
SocialId string `json:"social_id"`
Name string `json:"name"`
ProviderName string `json:"provider_name"`
ProviderType string `json:"provider_type,omitempty"`
}
Expand Down
10 changes: 8 additions & 2 deletions internal/client/social_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type socialProvidersResponse struct {
Data []SocialProvider `json:"data"`
}

func (c *client) GetSocialProvider(name string) (*SocialProvider, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/providers-srv/multi/providers/list?provider_name=%s&provider_type=system", c.HostUrl, name), nil)
func (c *client) GetSocialProvider(providerName string, name string) (*SocialProvider, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/providers-srv/multi/providers/list?provider_name=%s&provider_type=system", c.HostUrl, providerName), nil)

if err != nil {
return nil, err
Expand All @@ -32,6 +32,12 @@ func (c *client) GetSocialProvider(name string) (*SocialProvider, error) {
return nil, err
}

for _, provider := range response.Data {
if provider.Name == name {
return &provider, nil
}
}

if len(response.Data) != 1 {
return nil, errors.New("could not identify provider")
}
Expand Down
17 changes: 14 additions & 3 deletions internal/provider/data_source_social_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/real-digital/terraform-provider-cidaas/internal/util"
)

type socialProviderDataSource struct {
Expand All @@ -31,6 +32,9 @@ func (d *socialProviderDataSource) Schema(_ context.Context, _ datasource.Schema
"provider_name": schema.StringAttribute{
Required: true,
},
"name": schema.StringAttribute{
Optional: true,
},
},
}
}
Expand All @@ -44,18 +48,25 @@ func (d *socialProviderDataSource) Configure(_ context.Context, req datasource.C
}

func (d socialProviderDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var name string
var providerName string
var name *string

var state SocialProvider

diags := req.Config.GetAttribute(ctx, path.Root("provider_name"), &name)
diags := req.Config.GetAttribute(ctx, path.Root("provider_name"), &providerName)
diags = req.Config.GetAttribute(ctx, path.Root("name"), &name)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

socialProvider, err := d.provider.client.GetSocialProvider(name)
if name == nil {
name = util.ToStringPointer("default")
}

socialProvider, err := d.provider.client.GetSocialProvider(providerName, *name)

if err != nil {
resp.Diagnostics.AddError("Could not fetch social socialProvider",
Expand Down
1 change: 1 addition & 0 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type SocialProvider struct {
SocialId types.String `tfsdk:"social_id"`
ProviderName types.String `tfsdk:"provider_name"`
ProviderType types.String `tfsdk:"provider_type"`
Name types.String `tfsdk:"name"`
}

type ConsentInstance struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/resource_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (r *appResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
Optional: true,
},
},
},
},
Expand Down

0 comments on commit c28cac2

Please sign in to comment.