Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/vhosts
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-helmich committed Apr 16, 2024
2 parents d84b9bb + 1ea9eb7 commit 2c00572
Show file tree
Hide file tree
Showing 19 changed files with 5,483 additions and 2,107 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This provider offers the following resources:
and the following data sources:

- [`mittwald_systemsoftware`](https://registry.terraform.io/providers/mittwald/mittwald/latest/docs/data-sources/systemsoftware)
- [`mittwald_user`](https://registry.terraform.io/providers/mittwald/mittwald/latest/docs/data-sources/user)

Coming soon:

Expand Down
7 changes: 7 additions & 0 deletions api/mittwaldv2/client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type ClientBuilder interface {
Database() DatabaseClient
Cronjob() CronjobClient
Domain() DomainClient
User() UserClient
}

type clientBuilder struct {
Expand Down Expand Up @@ -41,3 +42,9 @@ func (b *clientBuilder) Domain() DomainClient {
client: b.internalClient,
}
}

func (b *clientBuilder) User() UserClient {
return &userClient{
client: b.internalClient,
}
}
3 changes: 1 addition & 2 deletions api/mittwaldv2/client_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mittwaldv2

import (
"bytes"
"context"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"net/http"
Expand Down Expand Up @@ -53,7 +52,7 @@ func (c *debuggingClient) Do(req *http.Request) (*http.Response, error) {
logFields["err"] = err
}

tflog.Debug(context.Background(), "executed request", logFields)
tflog.Debug(req.Context(), "executed request", logFields)

return res, err
}
Expand Down
14 changes: 14 additions & 0 deletions api/mittwaldv2/client_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type ProjectClient interface {
ListProjects(ctx context.Context) ([]DeMittwaldV1ProjectProject, error)
GetProject(ctx context.Context, projectID string) (*DeMittwaldV1ProjectProject, error)
CreateProjectOnServer(ctx context.Context, serverID string, body ProjectCreateProjectJSONRequestBody) (string, error)
DeleteProject(ctx context.Context, projectID string) error
PollProject(ctx context.Context, projectID string) (*DeMittwaldV1ProjectProject, error)
Expand Down Expand Up @@ -60,6 +61,19 @@ func (c *projectClient) ListProjects(ctx context.Context) ([]DeMittwaldV1Project
return nil, errUnexpectedStatus(response.StatusCode(), response.Body)
}

func (c *projectClient) GetProject(ctx context.Context, projectID string) (*DeMittwaldV1ProjectProject, error) {
response, err := c.client.ProjectGetProjectWithResponse(ctx, uuid.MustParse(projectID))
if err != nil {
return nil, err
}

if response.JSON200 != nil {
return response.JSON200, nil
}

return nil, errUnexpectedStatus(response.StatusCode(), response.Body)
}

func (c *projectClient) CreateProjectOnServer(ctx context.Context, serverID string, body ProjectCreateProjectJSONRequestBody) (string, error) {
response, err := c.client.ProjectCreateProjectWithResponse(
ctx,
Expand Down
72 changes: 72 additions & 0 deletions api/mittwaldv2/client_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package mittwaldv2

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

type UserClient interface {
GetCurrentUser(ctx context.Context) (*DeMittwaldV1UserUser, error)
GetUser(ctx context.Context, userID string) (*DeMittwaldV1UserUser, error)
}

type userClient struct {
client ClientWithResponsesInterface
}

func (c *userClient) GetCurrentUser(ctx context.Context) (*DeMittwaldV1UserUser, error) {
return c.GetUser(ctx, "self")
}

func (c *userClient) GetUser(ctx context.Context, userID string) (*DeMittwaldV1UserUser, error) {
// NOTE:
// It is necessary to work directly with the innards of the client here,
// because the generated client is incorrect at this point. This is related
// to a number of issues:
//
// - https://github.com/deepmap/oapi-codegen/issues/1429
// - https://github.com/deepmap/oapi-codegen/issues/1433
// - https://github.com/deepmap/oapi-codegen/issues/1029

clientWithResponses, ok := c.client.(*ClientWithResponses)
if !ok {
return nil, fmt.Errorf("unexpected client type: %T", c.client)
}

client, ok := clientWithResponses.ClientInterface.(*Client)
if !ok {
return nil, fmt.Errorf("unexpected client type: %T", clientWithResponses.ClientInterface)
}

serverURL, err := url.Parse(client.Server)
if err != nil {
return nil, fmt.Errorf("error parsing server URL: %w", err)
}
operationURL, err := serverURL.Parse(fmt.Sprintf("/v2/users/%s", userID))
if err != nil {
return nil, fmt.Errorf("error parsing operation URL: %w", err)
}

req, _ := http.NewRequest("GET", operationURL.String(), nil)
req = req.WithContext(ctx)
if err := client.applyEditors(ctx, req, nil); err != nil {
return nil, err
}
httpResp, err := client.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("error getting user: %w", err)
}

resp, err := ParseUserGetUserResponse(httpResp)
if err != nil {
return nil, fmt.Errorf("error getting user: %w", err)
}

if resp.JSON200 == nil {
return nil, errUnexpectedStatus(resp.StatusCode(), resp.Body)
}

return resp.JSON200, nil
}
3 changes: 3 additions & 0 deletions api/mittwaldv2/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package mittwaldv2
// NOTE: This needs a patched version of oapi-codegen; PR #1178 [1] is
// needed to generate the correct code for the Mittwald API.
//
// Run with `go generate -tags generate_api ./api/...` to generate the
// Mittwald API client.
//
// [1]: https://github.com/deepmap/oapi-codegen/pull/1178

//go:generate wget https://api.mittwald.de/openapi -Oopenapi.json
Expand Down
Loading

0 comments on commit 2c00572

Please sign in to comment.