Skip to content

Commit

Permalink
Add new method to Client to get service connector by name
Browse files Browse the repository at this point in the history
  • Loading branch information
htahir1 committed Oct 28, 2024
1 parent 9820886 commit 6da8546
Show file tree
Hide file tree
Showing 7 changed files with 1,733 additions and 151 deletions.
84 changes: 53 additions & 31 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type Client struct {
HTTPClient *http.Client
}

type ServerInfo struct {
Version string `json:"version"`
Metadata map[string]string `json:"metadata"`
}

func NewClient(serverURL, apiKey string) *Client {
return &Client{
ServerURL: serverURL,
Expand Down Expand Up @@ -97,6 +102,21 @@ func (c *Client) doRequest(method, path string, body interface{}) (*http.Respons
return resp, nil
}

// GetServerInfo fetches server info to determine version and capabilities
func (c *Client) GetServerInfo() (*ServerInfo, error) {
resp, err := c.doRequest("GET", "/api/v1/info", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var result ServerInfo
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("error decoding server info: %v", err)
}
return &result, nil
}

// Stack operations
func (c *Client) CreateStack(workspace string, stack StackRequest) (*StackResponse, error) {
// Check server version to determine which endpoint to use
Expand All @@ -105,9 +125,13 @@ func (c *Client) CreateStack(workspace string, stack StackRequest) (*StackRespon
return nil, fmt.Errorf("failed to get server info: %v", err)
}

log.Printf("Server info: %+v", info)

endpoint := fmt.Sprintf("/api/v1/workspaces/%s/stacks", workspace)
if isLowerVersion(info.Version, "0.65.0") {
endpoint = fmt.Sprintf("/api/v1/workspaces/%s/full-stack", workspace)

// Set workspace in request if not already set
if stack.Workspace == nil {
stack.Workspace = &workspace
}

resp, err := c.doRequest("POST", endpoint, stack)
Expand All @@ -123,32 +147,6 @@ func (c *Client) CreateStack(workspace string, stack StackRequest) (*StackRespon
return &result, nil
}

// GetServerInfo fetches server info to determine version and capabilities
func (c *Client) GetServerInfo() (*ServerInfo, error) {
resp, err := c.doRequest("GET", "/api/v1/info", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var result ServerInfo
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("error decoding server info: %v", err)
}
return &result, nil
}

type ServerInfo struct {
Version string `json:"version"`
Metadata map[string]string `json:"metadata"`
}

// Helper function to compare versions
func isLowerVersion(version, compare string) bool {
// Simple version comparison - in production you'd want a more robust version comparison
return version < compare
}

// Remaining methods from the original client...
func (c *Client) GetStack(id string) (*StackResponse, error) {
resp, err := c.doRequest("GET", fmt.Sprintf("/api/v1/stacks/%s", id), nil)
Expand Down Expand Up @@ -219,8 +217,11 @@ func (c *Client) ListStacks(params *ListParams) (*Page[StackResponse], error) {
}

// Component operations...
func (c *Client) CreateComponent(workspace string, body ComponentRequest) (*ComponentResponse, error) {
resp, err := c.doRequest("POST", fmt.Sprintf("/api/v1/workspaces/%s/components", workspace), body)
func (c *Client) CreateComponent(workspace string, component ComponentRequest) (*ComponentResponse, error) {
// Ensure workspace and user are set in the request
component.Workspace = workspace

resp, err := c.doRequest("POST", fmt.Sprintf("/api/v1/workspaces/%s/components", workspace), component)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -380,4 +381,25 @@ func (c *Client) ListServiceConnectors(params *ListParams) (*Page[ServiceConnect
}

return &result, nil
}
}

// Add this new method to the Client
func (c *Client) GetServiceConnectorByName(workspace, name string) (*ServiceConnectorResponse, error) {
params := &ListParams{
Filter: map[string]string{
"name": name,
"workspace": workspace,
},
}

connectors, err := c.ListServiceConnectors(params)
if err != nil {
return nil, err
}

if len(connectors.Items) == 0 {
return nil, fmt.Errorf("no service connector found with name %s", name)
}

return &connectors.Items[0], nil
}
13 changes: 7 additions & 6 deletions internal/provider/data_source_service_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func setServiceConnectorFields(d *schema.ResourceData, connector *ServiceConnect
if err := d.Set("name", connector.Name); err != nil {
return fmt.Errorf("error setting name: %v", err)
}
if err := d.Set("type", connector.Type); err != nil {
if err := d.Set("type", connector.Body.ConnectorType); err != nil {
return fmt.Errorf("error setting type: %v", err)
}
if err := d.Set("auth_method", connector.AuthMethod); err != nil {
if err := d.Set("auth_method", connector.Body.AuthMethod); err != nil {
return fmt.Errorf("error setting auth_method: %v", err)
}

Expand All @@ -67,10 +67,11 @@ func setServiceConnectorFields(d *schema.ResourceData, connector *ServiceConnect
return fmt.Errorf("error setting resource_types: %v", err)
}
}
if connector.Body.Workspace != "" {
if err := d.Set("workspace", connector.Body.Workspace); err != nil {
return fmt.Errorf("error setting workspace: %v", err)
}
}

if connector.Metadata != nil && connector.Metadata.Workspace != nil {
if err := d.Set("workspace", connector.Metadata.Workspace.Name); err != nil {
return fmt.Errorf("error setting workspace: %v", err)
}
}

Expand Down
11 changes: 2 additions & 9 deletions internal/provider/data_source_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,8 @@ func setStackData(d *schema.ResourceData, stack *StackResponse) error {
d.SetId(stack.ID)
d.Set("name", stack.Name)

components := make(map[string]string)
for k, v := range stack.Components {
components[k] = v.ID
}
d.Set("components", components)

if stack.Labels != nil {
d.Set("labels", stack.Labels)
}
d.Set("components", stack.Metadata.Components)
d.Set("labels", stack.Metadata.Labels)

return nil
}
40 changes: 22 additions & 18 deletions internal/provider/data_source_stack_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,30 @@ func dataSourceStackComponentRead(d *schema.ResourceData, m interface{}) error {
}

func setStackComponentFields(d *schema.ResourceData, component *ComponentResponse) error {
if component.Body == nil {
return fmt.Errorf("received empty response body")
if err := d.Set("name", component.Name); err != nil {
return fmt.Errorf("error setting name: %v", err)
}

// Access all fields through component.Body
d.Set("name", component.Body.Name)
d.Set("type", component.Body.Type)
d.Set("flavor", component.Body.Flavor)
d.Set("configuration", component.Body.Configuration)

if component.Body.Workspace != "" {
d.Set("workspace", component.Body.Workspace)
}

if component.Body.ConnectorResourceID != "" {
d.Set("connector_resource_id", component.Body.ConnectorResourceID)
}

if component.Body.Labels != nil {
d.Set("labels", component.Body.Labels)
if component.Metadata != nil {
if err := d.Set("configuration", component.Metadata.Configuration); err != nil {
return fmt.Errorf("error setting configuration: %v", err)
}

if component.Metadata.Workspace != nil {
if err := d.Set("workspace", component.Metadata.Workspace.Name); err != nil {
return fmt.Errorf("error setting workspace: %v", err)
}
}

if component.Metadata.ConnectorResourceID != nil {
if err := d.Set("connector_resource_id", component.Metadata.ConnectorResourceID); err != nil {
return fmt.Errorf("error setting connector_resource_id: %v", err)
}
}

if err := d.Set("labels", component.Metadata.Labels); err != nil {
return fmt.Errorf("error setting labels: %v", err)
}
}

return nil
Expand Down
Loading

0 comments on commit 6da8546

Please sign in to comment.