Skip to content

Commit

Permalink
feat: fetch endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
j0g3sc committed Nov 21, 2023
1 parent 8fa158d commit e5d4e2b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
75 changes: 68 additions & 7 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

const (
apiRoot = "/api"
loginEndpoint = "/api/login"
apiMeshObjectsRoot = "/api/meshobjects"
loginEndpoint = "/api/login"

ERROR_GENERIC_CLIENT_ERROR = "client error"
ERROR_AUTHENTICATION_FAILURE = "not authorized. check api key and secret."
ERROR_ENDPOINT_LOOKUP = "could not fetch endpoints for meshStack."
)
Expand All @@ -29,7 +30,7 @@ type MeshStackProviderClient struct {
}

type endpoints struct {
buildingBlocks string
BuildingBlocks string `json:"meshbuildingblocks"`
}

type loginResponse struct {
Expand Down Expand Up @@ -73,6 +74,11 @@ func (c *MeshStackProviderClient) login() error {
},
},
)

if err != nil {
return errors.New(ERROR_GENERIC_CLIENT_ERROR)
}

defer res.Body.Close()

if err != nil || res.StatusCode != 200 {
Expand Down Expand Up @@ -104,9 +110,64 @@ func (c *MeshStackProviderClient) lookUpEndpoints() error {
return errors.New(ERROR_AUTHENTICATION_FAILURE)
}

// TODO
return errors.New("not implemented")
meshObjectsPath, err := url.JoinPath(c.url.String(), apiMeshObjectsRoot)
if err != nil {
return err
}
meshObjects, _ := url.Parse(meshObjectsPath)

res, err := c.httpClient.Do(
&http.Request{
URL: meshObjects,
Method: "GET",
Header: http.Header{
"Authorization": {c.token},
},
},
)

if err != nil {
return errors.New(ERROR_GENERIC_CLIENT_ERROR)
}

defer res.Body.Close()

if res.StatusCode != 200 {
return errors.New(ERROR_AUTHENTICATION_FAILURE)
}

data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
var endpoints endpoints
json.Unmarshal(data, &endpoints)
c.endpoints = endpoints

return nil
}

// TODO
func (c *MeshStackProviderClient) ReadBuildingBlock() {}
func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*BuildingBlockResourceModel, error) {
if c.ensureValidToken() != nil {
return nil, errors.New(ERROR_AUTHENTICATION_FAILURE)
}

targetPath, err := url.JoinPath(c.url.String(), c.endpoints.BuildingBlocks, uuid)
if err != nil {
return nil, err
}

targetUrl, _ := url.Parse(targetPath)
_, err = c.httpClient.Do(
&http.Request{
URL: targetUrl,
Method: "GET",
Header: http.Header{
"Authorization": {c.token},
},
},
)

// TODO parse response into a data model and then into a BuildingBlockResourceModel
return nil, errors.New("not implemented")
}
4 changes: 1 addition & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package provider

import (
"context"
"net/http"
"net/url"
"time"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
Expand Down Expand Up @@ -61,7 +59,7 @@ func (p *MeshStackProvider) Configure(ctx context.Context, req provider.Configur
if err != nil {
resp.Diagnostics.AddError("Provider endpoint not valid.", "The value provided as the providers endpoint is not a valid URL.")
} else {
client := NewClient(url, data.ApiKey.ValueString(), data.ApiSecret.ValueString())
client, _ := NewClient(url, data.ApiKey.ValueString(), data.ApiSecret.ValueString()) // TODO handle err
resp.DataSourceData = client
resp.ResourceData = client
}
Expand Down

0 comments on commit e5d4e2b

Please sign in to comment.