Skip to content

Commit

Permalink
Update existing PF-implemented data sources to use fwresource.DataSou…
Browse files Browse the repository at this point in the history
…rceWithConfigure
  • Loading branch information
SarahFrench committed Oct 4, 2024
1 parent 9e2286c commit 56d9e08
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewGoogleFirebaseAndroidAppConfigDataSource() datasource.DataSource {

// GoogleFirebaseAndroidAppConfigDataSource defines the data source implementation
type GoogleFirebaseAndroidAppConfigDataSource struct {
client *firebase.Service
fwresource.DataSourceWithConfigure
providerConfig *transport_tpg.Config
}

Expand Down Expand Up @@ -85,28 +85,6 @@ func (d *GoogleFirebaseAndroidAppConfigDataSource) Schema(ctx context.Context, r
}
}

func (d *GoogleFirebaseAndroidAppConfigDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

p, ok := req.ProviderData.(*transport_tpg.Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *transport_tpg.Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.client = p.NewFirebaseClient(ctx, p.UserAgent)
if resp.Diagnostics.HasError() {
return
}
d.providerConfig = p
}

func (d *GoogleFirebaseAndroidAppConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data GoogleFirebaseAndroidAppConfigModel
var metaData *fwmodels.ProviderMetaModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewGoogleFirebaseAppleAppConfigDataSource() datasource.DataSource {

// GoogleFirebaseAppleAppConfigDataSource defines the data source implementation
type GoogleFirebaseAppleAppConfigDataSource struct {
client *firebase.Service
fwresource.DataSourceWithConfigure
providerConfig *transport_tpg.Config
}

Expand Down Expand Up @@ -85,28 +85,6 @@ func (d *GoogleFirebaseAppleAppConfigDataSource) Schema(ctx context.Context, req
}
}

func (d *GoogleFirebaseAppleAppConfigDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

p, ok := req.ProviderData.(*transport_tpg.Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *transport_tpg.Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.client = p.NewFirebaseClient(ctx, p.UserAgent)
if resp.Diagnostics.HasError() {
return
}
d.providerConfig = p
}

func (d *GoogleFirebaseAppleAppConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data GoogleFirebaseAppleAppConfigModel
var metaData *fwmodels.ProviderMetaModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewGoogleFirebaseWebAppConfigDataSource() datasource.DataSource {

// GoogleFirebaseWebAppConfigDataSource defines the data source implementation
type GoogleFirebaseWebAppConfigDataSource struct {
client *firebase.Service
fwresource.DataSourceWithConfigure
providerConfig *transport_tpg.Config
}

Expand Down Expand Up @@ -132,28 +132,6 @@ func (d *GoogleFirebaseWebAppConfigDataSource) Schema(ctx context.Context, req d
}
}

func (d *GoogleFirebaseWebAppConfigDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

p, ok := req.ProviderData.(*transport_tpg.Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *transport_tpg.Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.client = p.NewFirebaseClient(ctx, p.UserAgent)
if resp.Diagnostics.HasError() {
return
}
d.providerConfig = p
}

func (d *GoogleFirebaseWebAppConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data GoogleFirebaseWebAppConfigModel
var metaData *fwmodels.ProviderMetaModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewGoogleClientConfigDataSource() datasource.DataSource {
}

type GoogleClientConfigDataSource struct {
providerConfig *transport_tpg.Config
fwresource.DataSourceWithConfigure
}

type GoogleClientConfigModel struct {
Expand Down Expand Up @@ -96,25 +96,6 @@ func (d *GoogleClientConfigDataSource) Schema(ctx context.Context, req datasourc
}
}

func (d *GoogleClientConfigDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

p, ok := req.ProviderData.(*transport_tpg.Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *transport_tpg.Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

// Required for accessing project, region, zone and tokenSource
d.providerConfig = p
}

func (d *GoogleClientConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data GoogleClientConfigModel
var metaData *fwmodels.ProviderMetaModel
Expand All @@ -131,18 +112,18 @@ func (d *GoogleClientConfigDataSource) Read(ctx context.Context, req datasource.
return
}

locationInfo := data.GetLocationDescription(d.providerConfig)
locationInfo := data.GetLocationDescription(d.Config)
region, _ := locationInfo.GetRegion()
zone, _ := locationInfo.GetZone()

data.Id = types.StringValue(fmt.Sprintf("projects/%s/regions/%s/zones/%s", d.providerConfig.Project, region.String(), zone.String()))
data.Project = types.StringValue(d.providerConfig.Project)
data.Id = types.StringValue(fmt.Sprintf("projects/%s/regions/%s/zones/%s", d.Config.Project, region.String(), zone.String()))
data.Project = types.StringValue(d.Config.Project)
data.Region = region
data.Zone = zone

// Convert default labels from SDK type system to plugin-framework data type
m := map[string]*string{}
for k, v := range d.providerConfig.DefaultLabels {
for k, v := range d.Config.DefaultLabels {
// m[k] = types.StringValue(v)
val := v
m[k] = &val
Expand All @@ -155,7 +136,7 @@ func (d *GoogleClientConfigDataSource) Read(ctx context.Context, req datasource.

data.DefaultLabels = dls

token, err := d.providerConfig.TokenSource.Token()
token, err := d.Config.TokenSource.Token()
if err != nil {
resp.Diagnostics.AddError("Error setting access_token", err.Error())
return
Expand Down

0 comments on commit 56d9e08

Please sign in to comment.