Skip to content

Commit

Permalink
Merge pull request #16 from l-with/add-resource-oauth2-client
Browse files Browse the repository at this point in the history
add resource oauth2 client
  • Loading branch information
l-with authored Jul 31, 2022
2 parents a0b59ca + 3207e49 commit 09c93fa
Show file tree
Hide file tree
Showing 15 changed files with 605 additions and 12 deletions.
94 changes: 94 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ type ApiMailcowGetRequest struct {
xAPIKey *string
}

type ApiMailcowGetAllRequest struct {
ctx context.Context
ApiService *ApiService
endpoint string
xAPIKey *string
}

func (r ApiMailcowGetRequest) XAPIKey(xAPIKey string) ApiMailcowGetRequest {
r.xAPIKey = &xAPIKey
return r
Expand Down Expand Up @@ -351,6 +358,93 @@ func (a *ApiService) MailcowGetExecute(r ApiMailcowGetRequest) (*http.Response,
return localVarHTTPResponse, nil
}

func (a *ApiService) MailcowGetAllExecute(r ApiMailcowGetAllRequest) (*http.Response, error) {
var (
localVarHTTPMethod = http.MethodGet
localVarPostBody interface{}
formFiles []formFile
)

localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "ApiService.MailcowGet")
if err != nil {
return nil, &GenericOpenAPIError{error: err.Error()}
}

localVarPath := localBasePath + r.endpoint

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}

// to determine the Content-Type header
localVarHTTPContentTypes := []string{}

// set Content-Type header
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}

// to determine the Accept header
localVarHTTPHeaderAccepts := []string{"application/json"}

// set Accept header
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
if r.xAPIKey != nil {
localVarHeaderParams["X-API-Key"] = parameterToString(*r.xAPIKey, "")
}
if r.ctx != nil {
// API Key Authentication
if auth, ok := r.ctx.Value(ContextAPIKeys).(map[string]APIKey); ok {
if apiKey, ok := auth["ApiKeyAuth"]; ok {
var key string
if apiKey.Prefix != "" {
key = apiKey.Prefix + " " + apiKey.Key
} else {
key = apiKey.Key
}
localVarHeaderParams["X-API-Key"] = key
}
}
}
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
if err != nil {
return nil, err
}

localVarHTTPResponse, err := a.client.callAPI(req)
if err != nil || localVarHTTPResponse == nil {
return localVarHTTPResponse, err
}

localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body)
localVarHTTPResponse.Body.Close()
localVarHTTPResponse.Body = ioutil.NopCloser(bytes.NewBuffer(localVarBody))
if err != nil {
return localVarHTTPResponse, err
}

if localVarHTTPResponse.StatusCode >= 300 {
newErr := &GenericOpenAPIError{
body: localVarBody,
error: localVarHTTPResponse.Status,
}
var v ErrorResponse
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarHTTPResponse, newErr
}
newErr.model = v
return localVarHTTPResponse, newErr
}

return localVarHTTPResponse, nil
}

type ApiMailcowUpdateRequest struct {
ctx context.Context
ApiService *ApiService
Expand Down
8 changes: 4 additions & 4 deletions api/model_alias_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
)

func (o *MailcowResponseArray) GetId() (error, *string) {
func (o *MailcowResponseArray) GetAliasId() (*string, error) {
if !o.HasFinalMsgItem(0) || !o.HasFinalMsgItem(2) {
return errors.New(fmt.Sprint("msg error: ", o.GetFinalMsgs())), nil
return nil, errors.New(fmt.Sprint("msg error: ", o.GetFinalMsgs()))
}
receipt := *o.GetFinalMsgItem(0)
if receipt != "alias_added" {
return errors.New(fmt.Sprint("msg error: ", receipt)), nil
return nil, errors.New(fmt.Sprint("msg error: ", receipt))
}
return nil, o.GetFinalMsgItem(2)
return o.GetFinalMsgItem(2), nil
}
10 changes: 9 additions & 1 deletion api/model_create_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func NewCreateSyncjobRequest() *MailcowCreateRequest {
return &this
}

func NewCreateOAuth2ClientRequest() *MailcowCreateRequest {
this := MailcowCreateRequest{}
this.payload = make(map[string]interface{})
this.endpoint = "/api/v1/add/oauth2-client"
this.ResourceName = "resourceOAuth2Client"
return &this
}

func (o *MailcowCreateRequest) Get(key string) interface{} {
if !o.Has(key) {
var ret bool
Expand Down Expand Up @@ -129,7 +137,7 @@ func (o MailcowCreateRequest) MarshalJSON(requestSpec map[string]interface{}) ([
//if o.attr != nil {
// toSerialize["attr"] = o.attr
//}
for key, _ := range requestSpec {
for key := range requestSpec {
//key := element.(map)
if o.payload[key] != nil {
toSerialize[key] = o.payload[key]
Expand Down
7 changes: 7 additions & 0 deletions api/model_delete_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func NewDeleteSyncjobRequest() *MailcowDeleteRequest {
return &this
}

func NewDeleteOAuth2ClientRequest() *MailcowDeleteRequest {
this := MailcowDeleteRequest{}
this.endpoint = "/api/v1/delete/oauth2-client"
this.ResourceName = "resourceOAuth2Client"
return &this
}

func (o *MailcowDeleteRequest) GetItem() *string {
log.Print("[TRACE] GetItem")
if !o.HasItem() {
Expand Down
17 changes: 17 additions & 0 deletions api/model_get_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ func (a *ApiService) MailcowGetSyncjob(ctx context.Context, id string) ApiMailco
id: id,
}
}

func (a *ApiService) MailcowGetOAuth2Client(ctx context.Context, id string) ApiMailcowGetRequest {
return ApiMailcowGetRequest{
ApiService: a,
ctx: ctx,
endpoint: "/api/v1/get/oauth2-client/{id}",
id: id,
}
}

func (a *ApiService) MailcowGetOAuth2Clients(ctx context.Context) ApiMailcowGetAllRequest {
return ApiMailcowGetAllRequest{
ApiService: a,
ctx: ctx,
endpoint: "/api/v1/get/oauth2-client/all",
}
}
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ This is under development. You will certainly find bugs and limitations. In thos
<!-- schema generated by tfplugindocs -->
## Schema

### Required
### Optional

- `api_key` (String, Sensitive) The mailcow API key, can optionally be passed as `MAILCOW_API_KEY` environmental variable
- `host_name` (String) The name of the mailcow host, can optionally be passed as `MAILCOW_HOST_NAME` environmental variable
37 changes: 37 additions & 0 deletions docs/resources/oauth2_client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
page_title: "mailcow_oauth2_client Resource - terraform-provider-mailcow"
subcategory: ""
description: |-
---

# mailcow_oauth2_client (Resource)

Provides an OAuth2 client for mailcow.
This can be used to create and delete OAuth2 clients for mailcow.

## Example Usage
```terraform
resource "mailcow_oauth2_client" "client" {
redirect_uri = "https:/redirect.uri"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `redirect_uri` (String)

### Read-Only

- `client_id` (String)
- `client_secret` (String, Sensitive)
- `id` (String) The ID of this resource.
- `scope` (String)

## Restriction

The mailcow API does not return the id of the OAuth2 client as response for creation.
As workaround the redirect uri is used as identifier for the resource.
As consequence creation of an OAuth2 client with an existing redirect uri is prohibited.
3 changes: 3 additions & 0 deletions examples/resources/mailcow_oauth2_client/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "mailcow_oauth2_client" "client" {
redirect_uri = "https:/redirect.uri"
}
11 changes: 6 additions & 5 deletions mailcow/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"mailcow_alias": resourceAlias(),
"mailcow_domain": resourceDomain(),
"mailcow_mailbox": resourceMailbox(),
"mailcow_dkim": resourceDkim(),
"mailcow_syncjob": resourceSyncjob(),
"mailcow_alias": resourceAlias(),
"mailcow_domain": resourceDomain(),
"mailcow_mailbox": resourceMailbox(),
"mailcow_dkim": resourceDkim(),
"mailcow_syncjob": resourceSyncjob(),
"mailcow_oauth2_client": resourceOAuth2Client(),
},
DataSourcesMap: map[string]*schema.Resource{
"mailcow_domain": dataSourceDomain(),
Expand Down
5 changes: 4 additions & 1 deletion mailcow/resource_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func resourceAliasCreate(ctx context.Context, d *schema.ResourceData, m interfac
return diag.FromErr(err)
}

err, id := response.GetId()
id, err := response.GetAliasId()
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -99,6 +99,9 @@ func resourceAliasRead(ctx context.Context, d *schema.ResourceData, m interface{
}

setResourceData(resourceAlias(), d, &alias, nil, nil)
if err != nil {
return diag.FromErr(err)
}

d.SetId(fmt.Sprint(alias["id"].(float64)))

Expand Down
Loading

0 comments on commit 09c93fa

Please sign in to comment.