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
18 changed files
with
723 additions
and
128 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,134 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type MeshProjectBinding struct { | ||
ApiVersion string `json:"apiVersion" tfsdk:"api_version"` | ||
Kind string `json:"kind" tfsdk:"kind"` | ||
Metadata MeshProjectBindingMetadata `json:"metadata" tfsdk:"metadata"` | ||
RoleRef MeshProjectRoleRef `json:"roleRef" tfsdk:"role_ref"` | ||
TargetRef MeshProjectTargetRef `json:"targetRef" tfsdk:"target_ref"` | ||
Subject MeshSubject `json:"subject" tfsdk:"subject"` | ||
} | ||
|
||
type MeshProjectBindingMetadata struct { | ||
Name string `json:"name" tfsdk:"name"` | ||
} | ||
|
||
type MeshProjectRoleRef struct { | ||
Name string `json:"name" tfsdk:"name"` | ||
} | ||
|
||
type MeshProjectTargetRef struct { | ||
Name string `json:"name" tfsdk:"name"` | ||
OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"` | ||
} | ||
|
||
type MeshSubject struct { | ||
Name string `json:"name" tfsdk:"name"` | ||
} | ||
|
||
func (c *MeshStackProviderClient) readProjectBinding(name string, contentType string) (*MeshProjectBinding, error) { | ||
var targetUrl *url.URL | ||
switch contentType { | ||
case CONTENT_TYPE_PROJECT_USER_BINDING: | ||
targetUrl = c.urlForPojectUserBinding(name) | ||
|
||
case CONTENT_TYPE_PROJECT_GROUP_BINDING: | ||
targetUrl = c.urlForPojectGroupBinding(name) | ||
|
||
default: | ||
return nil, fmt.Errorf("Unexpected content type: %s", contentType) | ||
} | ||
|
||
req, err := http.NewRequest("GET", targetUrl.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Accept", contentType) | ||
|
||
res, err := c.doAuthenticatedRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
data, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if res.StatusCode == 404 { | ||
return nil, nil | ||
} | ||
|
||
if res.StatusCode != 200 { | ||
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) | ||
} | ||
|
||
var binding MeshProjectBinding | ||
err = json.Unmarshal(data, &binding) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &binding, nil | ||
} | ||
|
||
func (c *MeshStackProviderClient) createProjectBinding(binding *MeshProjectBinding, contentType string) (*MeshProjectBinding, error) { | ||
var targetUrl *url.URL | ||
switch contentType { | ||
case CONTENT_TYPE_PROJECT_USER_BINDING: | ||
targetUrl = c.endpoints.ProjectUserBindings | ||
|
||
case CONTENT_TYPE_PROJECT_GROUP_BINDING: | ||
targetUrl = c.endpoints.ProjectGroupBindings | ||
|
||
default: | ||
return nil, fmt.Errorf("Unexpected content type: %s", contentType) | ||
} | ||
|
||
payload, err := json.Marshal(binding) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", targetUrl.String(), bytes.NewBuffer(payload)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Content-Type", CONTENT_TYPE_PROJECT_GROUP_BINDING) | ||
req.Header.Set("Accept", CONTENT_TYPE_PROJECT_GROUP_BINDING) | ||
|
||
res, err := c.doAuthenticatedRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
data, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if res.StatusCode != 200 { | ||
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) | ||
} | ||
|
||
var createdBinding MeshProjectBinding | ||
err = json.Unmarshal(data, &createdBinding) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &createdBinding, nil | ||
} |
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,26 @@ | ||
package client | ||
|
||
import ( | ||
"net/url" | ||
) | ||
|
||
const CONTENT_TYPE_PROJECT_GROUP_BINDING = "application/vnd.meshcloud.api.meshprojectgroupbinding.v3.hal+json" | ||
|
||
type MeshProjectGroupBinding = MeshProjectBinding | ||
|
||
func (c *MeshStackProviderClient) urlForPojectGroupBinding(name string) *url.URL { | ||
return c.endpoints.ProjectGroupBindings.JoinPath(name) | ||
} | ||
|
||
func (c *MeshStackProviderClient) ReadProjectGroupBinding(name string) (*MeshProjectGroupBinding, error) { | ||
return c.readProjectBinding(name, CONTENT_TYPE_PROJECT_GROUP_BINDING) | ||
} | ||
|
||
func (c *MeshStackProviderClient) CreateProjectGroupBinding(binding *MeshProjectGroupBinding) (*MeshProjectGroupBinding, error) { | ||
return c.createProjectBinding(binding, CONTENT_TYPE_PROJECT_GROUP_BINDING) | ||
} | ||
|
||
func (c *MeshStackProviderClient) DeleteProjecGroupBinding(name string) error { | ||
targetUrl := c.urlForPojectGroupBinding(name) | ||
return c.deleteMeshObject(*targetUrl, 204) | ||
} |
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.