generated from hashicorp/terraform-provider-scaffolding-framework
-
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.
- Loading branch information
Showing
16 changed files
with
940 additions
and
203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package dog | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
api "github.com/relaypro-open/dog_api_golang/api" | ||
) | ||
|
||
type ( | ||
profileDataSource struct { | ||
p dogProvider | ||
} | ||
|
||
ProfileList []Profile | ||
|
||
Profile struct { | ||
ID types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Rules *Rules `tfsdk:"rules"` | ||
Version types.String `tfsdk:"version"` | ||
} | ||
Rules struct { | ||
Inbound []*Rule `tfsdk:"inbound"` | ||
Outbound []*Rule `tfsdk:"outbound"` | ||
} | ||
|
||
Rule struct { | ||
Action types.String `tfsdk:"action"` | ||
Active types.Bool `tfsdk:"active"` | ||
Comment types.String `tfsdk:"comment"` | ||
Environments []string `tfsdk:"environments"` | ||
Group types.String `tfsdk:"group"` | ||
GroupType types.String `tfsdk:"group_type"` | ||
Interface types.String `tfsdk:"interface"` | ||
Log types.Bool `tfsdk:"log"` | ||
LogPrefix types.String `tfsdk:"log_prefix"` | ||
Order types.Int64 `tfsdk:"order"` | ||
Service types.String `tfsdk:"service"` | ||
States []string `tfsdk:"states"` | ||
Type types.String `tfsdk:"type"` | ||
} | ||
|
||
) | ||
|
||
var ( | ||
_ datasource.DataSource = (*profileDataSource)(nil) | ||
) | ||
|
||
func NewProfileDataSource() datasource.DataSource { | ||
return &profileDataSource{} | ||
} | ||
|
||
|
||
func (*profileDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_profile" | ||
} | ||
|
||
func (*profileDataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { | ||
return tfsdk.Schema{ | ||
// This description is used by the documentation generator and the language server. | ||
MarkdownDescription: "Profile data source", | ||
|
||
Attributes: map[string]tfsdk.Attribute{ | ||
"api_key": { | ||
MarkdownDescription: "Profile configurable attribute", | ||
Optional: true, | ||
Type: types.StringType, | ||
}, | ||
"id": { | ||
MarkdownDescription: "Profile identifier", | ||
Type: types.StringType, | ||
Computed: true, | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (d *profileDataSource) 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.(*api.Client) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *dog.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.p.dog = client | ||
} | ||
|
||
type profileDataSourceData struct { | ||
ApiKey types.String `tfsdk:"api_key"` | ||
Id types.String `tfsdk:"id"` | ||
} | ||
|
||
//type profileDataSource struct { | ||
// provider provider | ||
//} | ||
|
||
func (d *profileDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state ProfileList | ||
|
||
res, statusCode, err := d.p.dog.GetProfiles(nil) | ||
if (statusCode < 200 || statusCode > 299) && statusCode != 404 { | ||
resp.Diagnostics.AddError("Client Unsuccesful", fmt.Sprintf("Status Code: %d", statusCode)) | ||
} | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read profiles, got error: %s", err)) | ||
} | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Set state | ||
for _, api_profile := range res { | ||
profile := ApiToProfile(api_profile) | ||
state = append(state, profile) | ||
} | ||
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
Oops, something went wrong.