Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add godoc comments #78

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions hcx/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,30 @@ import (
"net/http"
)

// ActivateBody represents the structure of the request body used for activation actions.
type ActivateBody struct {
Data ActivateData `json:"data"`
}

// ActivateData represents the detailed activation data, which includes a list of activation items.
type ActivateData struct {
Items []ActivateDataItem `json:"items"`
}

// ActivateDataItem represents an individual activation item, containing its specific configuration details.
type ActivateDataItem struct {
Config ActivateDataItemConfig `json:"config"`
}

// ActivateDataItemConfig represents the configuration details for a specific activation item.
type ActivateDataItemConfig struct {
URL string `json:"url"`
ActivationKey string `json:"activationKey"`
UUID string `json:"UUID,omitempty"`
}

// PostActivate ...
// PostActivate sends a request to activate a configuration using the provided body and returns the resulting
// ActivateBody object. Returns an error if the request fails or the response cannot be parsed.
func PostActivate(c *Client, body ActivateBody) (ActivateBody, error) {

resp := ActivateBody{}
Expand Down Expand Up @@ -64,7 +69,8 @@ func PostActivate(c *Client, body ActivateBody) (ActivateBody, error) {
return resp, nil
}

// GetActivate ...
// GetActivate sends a request to retrieve the current activation configuration and returns the resulting ActivateBody
// object. Returns an error if the request fails or the response cannot be parsed.
func GetActivate(c *Client) (ActivateBody, error) {

resp := ActivateBody{}
Expand Down Expand Up @@ -92,7 +98,9 @@ func GetActivate(c *Client) (ActivateBody, error) {
return resp, nil
}

// DeleteActivate ...
// DeleteActivate sends a request to remove the activation configuration using the provided body and returns the
// resulting ActivateBody object. Returns an error if the request fails or the response cannot be parsed.

func DeleteActivate(c *Client, body ActivateBody) (ActivateBody, error) {

resp := ActivateBody{}
Expand Down
5 changes: 4 additions & 1 deletion hcx/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ import (
"net/http"
)

// InsertCertificateBody represents the body structure used to insert a certificate.
type InsertCertificateBody struct {
Certificate string `json:"certificate"`
}

// InsertCertificateResult represents the result of inserting a certificate, including success and completion status.
type InsertCertificateResult struct {
Success bool `json:"success"`
Completed bool `json:"completed"`
}

// InsertL2Extention ...
// InsertCertificate sends a request to create a new certificate using the provided body and returns an
// InsertCertificateResult object. Returns an error if the request fails or the response cannot be parsed.
func InsertCertificate(c *Client, body InsertCertificateBody) (InsertCertificateResult, error) {

resp := InsertCertificateResult{}
Expand Down
23 changes: 17 additions & 6 deletions hcx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"time"
)

// Client -
// Client represents a structure for managing HTTP communication and authentication details.
type Client struct {
HostURL string
HTTPClient *http.Client
Expand All @@ -30,28 +30,31 @@ type Client struct {
AllowUnverifiedSSL bool
}

// AuthStruct -
// AuthStruct represents a structure containing username and password for authentication purposes.
type AuthStruct struct {
Username string `json:"username"`
Password string `json:"password"`
}

// AuthResponse -
// AuthResponse represents the structure of a response returned after a successful authentication.
type AuthResponse struct {
UserID int `json:"user_id"`
Username string `json:"username"`
Token string `json:"token"`
}

// Content represents a reusable structure containing a slice of strings, typically used for XML unmarshaling.
type Content struct {
Strings []string `xml:"string"`
}

// Entries represents a collection of content entries, typically used for XML unmarshaling.
type Entries struct {
Entry []Content `xml:"entry"`
}

// HCX Authentication
// HcxConnectorAuthenticate authenticates the client with the HCX service by sending a request with user credentials.
// It retrieves and stores the HCX authorization token required for subsequent requests.
func (c *Client) HcxConnectorAuthenticate() error {

rb, err := json.Marshal(AuthStruct{
Expand Down Expand Up @@ -126,14 +129,15 @@ func (c *Client) HcxConnectorAuthenticate() error {

}

// parse response header
// Parse response header.
c.Token = resp.Header.Get("x-hm-authorization")

return nil

}

// NewClient -
// NewClient initializes and returns a new Client instance with the provided configuration, including authentication
// details, HCX URL, and SSL settings.
func NewClient(hcx, username *string, password *string, adminUsername *string, adminPassword *string, allowUnverifiedSSL *bool, vmcToken *string) (*Client, error) {
c := Client{
HTTPClient: &http.Client{
Expand All @@ -152,6 +156,8 @@ func NewClient(hcx, username *string, password *string, adminUsername *string, a
return &c, nil
}

// doRequest performs an authenticated HTTP request. If the client is not yet authenticated, it performs authentication
// first, then executes the request. Returns the HTTP response, response body, and any encountered error.
func (c *Client) doRequest(req *http.Request) (*http.Response, []byte, error) {

if !c.IsAuthenticated {
Expand Down Expand Up @@ -192,6 +198,9 @@ func (c *Client) doRequest(req *http.Request) (*http.Response, []byte, error) {
return res, body, err
}

// doAdminRequest executes an HTTP request using the admin credentials for Basic Authentication. It supports requests
// that require elevated permissions and optionally skips SSL verification. Returns the response, response body, and any
// encountered error.
func (c *Client) doAdminRequest(req *http.Request) (*http.Response, []byte, error) {

req.Header.Set("Accept", "application/json")
Expand Down Expand Up @@ -232,6 +241,8 @@ func (c *Client) doAdminRequest(req *http.Request) (*http.Response, []byte, erro
return res, body, err
}

// doVmcRequest sends an HTTP request to the VMware Cloud (VMC) service. It uses the HCX token for authorization if
// present and optionally skips SSL verification. Returns the response, response body, and any encountered error.
func (c *Client) doVmcRequest(req *http.Request) (*http.Response, []byte, error) {

req.Header.Set("Accept", "application/json")
Expand Down
27 changes: 22 additions & 5 deletions hcx/compute_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
)

// InsertComputeProfileBody represents the body structure for inserting a compute profile.
type InsertComputeProfileBody struct {
Computes []Compute `json:"compute"`
ComputeProfileID string `json:"computeProfileId"`
Expand All @@ -23,6 +24,7 @@ type InsertComputeProfileBody struct {
Switches []Switch `json:"switches"`
}

// Compute represents the compute resource configuration.
type Compute struct {
ComputeID string `json:"cmpId"`
ComputeName string `json:"cmpName"`
Expand All @@ -32,6 +34,7 @@ type Compute struct {
Type string `json:"type"`
}

// Storage represents a storage entity in a compute profile configuration.
type Storage struct {
ComputeID string `json:"cmpId"`
ComputeName string `json:"cmpName"`
Expand All @@ -41,13 +44,15 @@ type Storage struct {
Type string `json:"type"`
}

// DeploymentContainer represents a container holding deployment configuration.
type DeploymentContainer struct {
Computes []Compute `json:"compute"`
CPUReservation int `json:"cpuReservation"`
MemoryReservation int `json:"memoryReservation"`
Storage []Storage `json:"storage"`
}

// Network represents a network entity with associated details.
type Network struct {
Name string `json:"name"`
ID string `json:"id"`
Expand All @@ -56,14 +61,17 @@ type Network struct {
Tags []string `json:"tags"`
}

// Status represents the current state of an entity, typically used to indicate its operational or lifecycle state.
type Status struct {
State string `json:"state"`
}

// Service represents a service with a specific name in a system configuration.
type Service struct {
Name string `json:"name"`
}

// Switch represents a network switch with identifiable attributes.
type Switch struct {
ComputeID string `json:"cmpId"`
ID string `json:"id"`
Expand All @@ -72,19 +80,23 @@ type Switch struct {
Type string `json:"type"`
}

// InsertComputeProfileResult represents the result of an operation to insert a compute profile.
type InsertComputeProfileResult struct {
Data InsertComputeProfileResultData `json:"data"`
}

// InsertComputeProfileResultData represents the result data for inserting a compute profile.
type InsertComputeProfileResultData struct {
InterconnectTaskID string `json:"interconnectTaskId"`
ComputeProfileID string `json:"computeProfileId"`
}

// GetComputeProfileResult represents a collection of compute profile details.
type GetComputeProfileResult struct {
Items []GetComputeProfileResultItem `json:"items"`
}

// GetComputeProfileResultItem represents the details of a compute profile.
type GetComputeProfileResultItem struct {
ComputeProfileID string `json:"computeProfileId"`
Name string `json:"name"`
Expand All @@ -96,7 +108,8 @@ type GetComputeProfileResultItem struct {
Switches []Switch `json:"switches"`
}

// InsertComputeProfile ...
// InsertComputeProfile sends a request to create a new compute profile using the provided body and returns an
// InsertComputeProfileResult object. Returns an error if the request fails or the response cannot be parsed.
func InsertComputeProfile(c *Client, body InsertComputeProfileBody) (InsertComputeProfileResult, error) {

resp := InsertComputeProfileResult{}
Expand Down Expand Up @@ -131,12 +144,14 @@ func InsertComputeProfile(c *Client, body InsertComputeProfileBody) (InsertCompu
return resp, nil
}

// DeleteComputeProfile ...
func DeleteComputeProfile(c *Client, computeprofileID string) (InsertComputeProfileResult, error) {
// DeleteComputeProfile sends a request to delete a specific compute profile identified by computeProfileID and an
// InsertComputeProfileResult object indicating the result of the operation. Returns an error if the request fails or
// the response cannot be parsed.
func DeleteComputeProfile(c *Client, computeProfileID string) (InsertComputeProfileResult, error) {

resp := InsertComputeProfileResult{}

req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/hybridity/api/interconnect/computeProfiles/%s", c.HostURL, computeprofileID), nil)
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/hybridity/api/interconnect/computeProfiles/%s", c.HostURL, computeProfileID), nil)
if err != nil {
fmt.Println(err)
return resp, err
Expand All @@ -159,7 +174,9 @@ func DeleteComputeProfile(c *Client, computeprofileID string) (InsertComputeProf
return resp, nil
}

// GetComputeProfile ...
// GetComputeProfile retrieves the details of a compute profile using the provided endpointID and computeProfileName,
// returning a GetComputeProfileResultItem object for the matching profile. Returns an error if the request fails, the
// response cannot be parsed, or no matching profile is found.
func GetComputeProfile(c *Client, endpointID string, computeProfileName string) (GetComputeProfileResultItem, error) {

resp := GetComputeProfileResult{}
Expand Down
1 change: 1 addition & 0 deletions hcx/data_source_network_backing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package hcx

import (
"context"
"fmt"

"github.com/vmware/terraform-provider-hcx/hcx/constants"
"github.com/vmware/terraform-provider-hcx/hcx/validators"
Expand Down
10 changes: 7 additions & 3 deletions hcx/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"net/http"
)

// AppEngineStartStopResult represents the result of an App Engine start or stop operation.
type AppEngineStartStopResult struct {
Result string `json:"result"`
}

// AppEngineStart ...
// AppEngineStart sends a request to start the App Engine component and returns the resulting AppEngineStartStopResult
// object. Returns an error if the request fails or the response cannot be parsed.
func AppEngineStart(c *Client) (AppEngineStartStopResult, error) {

resp := AppEngineStartStopResult{}
Expand Down Expand Up @@ -42,7 +44,8 @@ func AppEngineStart(c *Client) (AppEngineStartStopResult, error) {
return resp, nil
}

// AppEngineStop ...
// AppEngineStop sends a request to stop the App Engine component and returns the resulting AppEngineStartStopResult
// object. Returns an error if the request fails or the response cannot be parsed.
func AppEngineStop(c *Client) (AppEngineStartStopResult, error) {

resp := AppEngineStartStopResult{}
Expand Down Expand Up @@ -70,7 +73,8 @@ func AppEngineStop(c *Client) (AppEngineStartStopResult, error) {
return resp, nil
}

// GetAppEngineStatus ...
// GetAppEngineStatus sends a GET request to retrieve the current status of the App Engine component and returns the
// resulting AppEngineStartStopResult object. Returns an error if the request fails or the response cannot be parsed.
func GetAppEngineStatus(c *Client) (AppEngineStartStopResult, error) {

resp := AppEngineStartStopResult{}
Expand Down
Loading
Loading