Skip to content

Commit

Permalink
fix some typing
Browse files Browse the repository at this point in the history
  • Loading branch information
JensWalter committed Sep 18, 2024
1 parent e366719 commit bc79a1c
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 36 deletions.
186 changes: 180 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package provider

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
Expand All @@ -23,12 +26,12 @@ func New(version string) func() provider.Provider {

type dtzProvider struct {
version string
ApiKey string `tfsdk:"api_key"`
EnableServiceContainers bool `tfsdk:"enable_service_containers"`
EnableServiceObjectstore bool `tfsdk:"enable_service_objectstore"`
EnableServiceContainerregistry bool `tfsdk:"enable_service_containerregistry"`
EnableServiceRss2email bool `tfsdk:"enable_service_rss2email"`
EnableServiceObservability bool `tfsdk:"enable_service_observability"`
ApiKey string `tfsdk:"api_key"`
EnableServiceContainers types.Bool `tfsdk:"enable_service_containers"`
EnableServiceObjectstore types.Bool `tfsdk:"enable_service_objectstore"`
EnableServiceContainerregistry types.Bool `tfsdk:"enable_service_containerregistry"`
EnableServiceRss2email types.Bool `tfsdk:"enable_service_rss2email"`
EnableServiceObservability types.Bool `tfsdk:"enable_service_observability"`
}

func (p *dtzProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand Down Expand Up @@ -75,6 +78,62 @@ func (p *dtzProvider) Configure(ctx context.Context, req provider.ConfigureReque
if resp.Diagnostics.HasError() {
return
}

if config.EnableServiceRss2email.ValueBool() {
err := enableRss2EmailService(ctx, config.ApiKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to enable RSS2Email service",
fmt.Sprintf("An error occurred when calling the enable endpoint: %s", err),
)
return
}
}

if config.EnableServiceContainers.ValueBool() {
err := enableContainersService(ctx, config.ApiKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to enable Containers service",
fmt.Sprintf("An error occurred when calling the enable endpoint: %s", err),
)
return
}
}

if config.EnableServiceObjectstore.ValueBool() {
err := enableObjectstoreService(ctx, config.ApiKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to enable Objectstore service",
fmt.Sprintf("An error occurred when calling the enable endpoint: %s", err),
)
return
}
}

if config.EnableServiceContainerregistry.ValueBool() {
err := enableContainerregistryService(ctx, config.ApiKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to enable Container Registry service",
fmt.Sprintf("An error occurred when calling the enable endpoint: %s", err),
)
return
}
}

if config.EnableServiceObservability.ValueBool() {
err := enableObservabilityService(ctx, config.ApiKey)
if err != nil {
resp.Diagnostics.AddError(
"Unable to enable Observability service",
fmt.Sprintf("An error occurred when calling the enable endpoint: %s", err),
)
return
}
}

resp.DataSourceData = config
resp.ResourceData = config
}
Expand All @@ -96,3 +155,118 @@ func (p *dtzProvider) Resources(_ context.Context) []func() resource.Resource {
newContainersServiceResource,
}
}

func enableRss2EmailService(ctx context.Context, apiKey string) error {
url := "https://rss2email.dtz.rocks/api/2021-02-01/enable"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

req.Header.Set("X-API-KEY", apiKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

return nil
}

func enableContainersService(ctx context.Context, apiKey string) error {
url := "https://containers.dtz.rocks/api/2021-02-21/enable"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

req.Header.Set("X-API-KEY", apiKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

return nil
}

func enableObjectstoreService(ctx context.Context, apiKey string) error {
url := "https://objectstore.dtz.rocks/api/2022-11-28/enable"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

req.Header.Set("X-API-KEY", apiKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

return nil
}

func enableContainerregistryService(ctx context.Context, apiKey string) error {
url := "https://containerregistry.dtz.rocks/api/2023-12-28/enable"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

req.Header.Set("X-API-KEY", apiKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

return nil
}

func enableObservabilityService(ctx context.Context, apiKey string) error {
url := "https://observability.dtz.rocks/api/2021-02-01/enable"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

req.Header.Set("X-API-KEY", apiKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

return nil
}
67 changes: 37 additions & 30 deletions internal/provider/rss2email_feed_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,56 +40,63 @@ type createFeedRequest struct {

// Create implements resource.Resource.
func (d *rss2emailFeedResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
tflog.Info(ctx, "rss2emailFeedResource create")
var url = "https://rss2email.dtz.rocks/api/2021-02-01/rss2email/feed"
var createFeed = &createFeedRequest{
Url: "url",
Enabled: false,
var plan rss2emailFeedResource
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

createReq := createFeedRequest{
Url: plan.Url.ValueString(),
Enabled: plan.Enabled.ValueBool(),
}
rb, err := json.Marshal(createFeed)

body, err := json.Marshal(createReq)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create feed, got error: %s", err))
return
}
tflog.Info(ctx, "query API "+url)
request, err := http.NewRequest(http.MethodPost, url, strings.NewReader(string(rb)))

url := "https://rss2email.dtz.rocks/api/2021-02-01/rss2email/feed"
httpReq, err := http.NewRequest(http.MethodPost, url, strings.NewReader(string(body)))
if err != nil {
tflog.Error(ctx, "error retrieving")
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create request, got error: %s", err))
return
}
request.Header.Set("X-API-KEY", d.api_key)

httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("X-API-KEY", d.api_key)

client := &http.Client{}
response, err := client.Do(request)
httpResp, err := client.Do(httpReq)
if err != nil {
tflog.Error(ctx, "error fetching")
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create feed, got error: %s", err))
return
}
defer response.Body.Close()
defer httpResp.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
tflog.Error(ctx, "error reading")
if httpResp.StatusCode != http.StatusOK {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create feed, status code: %d", httpResp.StatusCode))
return
}
tflog.Info(ctx, fmt.Sprintf("rssFeedResource Read status: %d, body: %s", response.StatusCode, string(body[:])))

var resp_type rss2emailFeedResponse
err = json.Unmarshal(body, &resp_type)
var createResp rss2emailFeedResponse
err = json.NewDecoder(httpResp.Body).Decode(&createResp)
if err != nil {
tflog.Error(ctx, "error unmarshalling")
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to parse create response, got error: %s", err))
return
}
tflog.Info(ctx, fmt.Sprintf("rssFeedResource Read response: %+v", resp_type))

var state rss2emailFeedResource
state.Id = types.StringValue(resp_type.Id)
state.Url = types.StringValue(resp_type.Url)
state.Name = types.StringValue(resp_type.Name)
state.Enabled = types.BoolValue(resp_type.Enabled)
state.LastCheck = types.StringValue(resp_type.LastDataFound)
state.LastDataFound = types.StringValue(resp_type.LastDataFound)
// set state
diags := resp.State.Set(ctx, &state)
// Set the resource state
plan.Id = types.StringValue(createResp.Id)
plan.Name = types.StringValue(createResp.Name)
plan.LastCheck = types.StringValue(createResp.LastCheck)
plan.LastDataFound = types.StringValue(createResp.LastDataFound)
plan.Enabled = types.BoolValue(createResp.Enabled)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down

0 comments on commit bc79a1c

Please sign in to comment.