-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce code duplication in Framework (#167)
* Replace all "_" with defaultCredentialDomain * Add helpers to reduce code duplication
- Loading branch information
Showing
13 changed files
with
227 additions
and
266 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package jenkins | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
) | ||
|
||
type ( | ||
// dataSourceHelper provides assistive snippets of logic to help reduce duplication in | ||
// each data source definition. | ||
dataSourceHelper struct { | ||
client *jenkinsAdapter | ||
} | ||
) | ||
|
||
func newDataSourceHelper() *dataSourceHelper { | ||
return &dataSourceHelper{} | ||
} | ||
|
||
// Configure should register the client for the resource. | ||
func (d *dataSourceHelper) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*jenkinsAdapter) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
fmt.Sprintf("Expected *jenkinsAdapter, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *dataSourceHelper) schema(s map[string]schema.Attribute) map[string]schema.Attribute { | ||
s["id"] = schema.StringAttribute{ | ||
Computed: true, | ||
MarkdownDescription: "The full canonical job path, e.g. `/job/job-name`", | ||
} | ||
s["name"] = schema.StringAttribute{ | ||
Required: true, | ||
MarkdownDescription: "The name of the resource being read.", | ||
} | ||
s["folder"] = schema.StringAttribute{ | ||
MarkdownDescription: "The folder namespace containing this resource.", | ||
Optional: true, | ||
} | ||
s["description"] = schema.StringAttribute{ | ||
MarkdownDescription: "A human readable description of the credentials being stored.", | ||
Computed: true, | ||
} | ||
|
||
return s | ||
} | ||
|
||
func (d *dataSourceHelper) schemaCredential(s map[string]schema.Attribute) map[string]schema.Attribute { | ||
s = d.schema(s) | ||
s["domain"] = schema.StringAttribute{ | ||
MarkdownDescription: "The domain store containing this resource.", | ||
Optional: true, | ||
} | ||
s["scope"] = schema.StringAttribute{ | ||
MarkdownDescription: `The visibility of the credentials to Jenkins agents. This will be either "GLOBAL" or "SYSTEM".`, | ||
Computed: true, | ||
} | ||
|
||
return s | ||
} |
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
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
Oops, something went wrong.