diff --git a/background/main.go b/background/main.go index 9a1e10a..2b80d8e 100644 --- a/background/main.go +++ b/background/main.go @@ -37,8 +37,8 @@ func main() { } func initMarketplace() { - mkplHost := viper.GetString("marketplace.host") - //mkplHost := "localhost:8010" //for local + //mkplHost := viper.GetString("marketplace.host") + mkplHost := "localhost:8010" //for local // initialize Agent config config := agent.NewConfig() @@ -79,9 +79,9 @@ func initMarketplace() { func StartMonitoring() { log.Println("Agent Monitoring process has started....") - if err := Marketplace.AgentLogin(); err != nil { - log.Fatalf("Marketplace Agent login failed %v", err) - } + //if err := Marketplace.AgentLogin(); err != nil { + // log.Fatalf("Marketplace Agent login failed %v", err) + //} startPoller() log.Println("Agent process has completed") } diff --git a/cmd/background_process.go b/cmd/background_process.go index db191a1..520c9c8 100644 --- a/cmd/background_process.go +++ b/cmd/background_process.go @@ -16,6 +16,7 @@ func init() { AgentProcessCmd.AddCommand(StartProcessCmd) AgentProcessCmd.AddCommand(StopProcessCmd) AgentProcessCmd.AddCommand(RegisterVCenterCmd) + AgentProcessCmd.AddCommand(SubscribeCmd) } var AgentProcessCmd = &cobra.Command{ @@ -98,6 +99,29 @@ var RegisterVCenterCmd = &cobra.Command{ }, } +var SubscribeCmd = &cobra.Command{ + Use: "subscribe", + Short: "Subscribe solutions published in Marketplace using the agent process", + Long: "Subscribe solutions published in Marketplace using the agent process", + Args: cobra.NoArgs, + //PreRunE: GetRefreshToken, + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + onPremSub := captureSubscribeInput() + + log.Println("@@-- Marketplace instance @@-- ", Marketplace) + err := Marketplace.Subscribe(onPremSub) + if err != nil { + log.Printf("failed to subscribe the product error :%v\n", err) + return err + } + + Output.PrintHeader(fmt.Sprintf("Product :%s is successfully subscribed with Marketplace", onPremSub.ProductID)) + + return nil + }, +} + func captureCliInput() *models.VCenterRegistration { var username, password, host, vcenterID, contentLibName, datastoreID string fmt.Print("vcenter-ID (optional): ") @@ -130,6 +154,30 @@ func captureCliInput() *models.VCenterRegistration { return onPremReg } +func captureSubscribeInput() *models.OnPremSubscriptionRequest { + var productId, version, catalogName, onPremRegistrationId string + fmt.Print("Product ID (from Marketplace): ") + fmt.Scanln(&productId) + + fmt.Print("version (from Marketplace): ") + fmt.Scanln(&version) + + fmt.Print("Content Library Name: ") + fmt.Scanln(&catalogName) + + fmt.Print("Vcenter Registration ID: ") + fmt.Scanln(&onPremRegistrationId) + + sub := &models.OnPremSubscriptionRequest{ + ProductID: productId, + Version: version, + ContentLibraryName: catalogName, + VCenterRegistrationID: onPremRegistrationId, + } + + return sub +} + func GenerateUUID() string { uu1, _ := uuid.NewV4() uuidNew := fmt.Sprintf("%s", uu1) diff --git a/internal/models/agent.go b/internal/models/agent.go index 1a85660..c640236 100644 --- a/internal/models/agent.go +++ b/internal/models/agent.go @@ -77,3 +77,21 @@ type DefaultResponse struct { Message string `json:"message"` StatusCode int `json:"statuscode"` } + +type OnPremSubscriptionRequest struct { + ProductID string `json:"productId"` + Version string `json:"version"` + ContentLibraryName string `json:"catalogName"` + VCenterRegistrationID string `json:"onPremRegistrationId"` +} + +type OnPremSubscriptionResponse struct { + Message string `json:"message"` + StatusCode int `json:"statusCode"` + SubscriptionDetails []struct { + ProductId string `json:"productId"` + Version string `json:"version"` + SubscriptionId string `json:"subscriptionId"` + Status string `json:"status"` + } `json:"subscriptionDetails"` +} diff --git a/pkg/agent.go b/pkg/agent.go index 72e670d..e9d78b5 100644 --- a/pkg/agent.go +++ b/pkg/agent.go @@ -119,6 +119,16 @@ func (m *Marketplace) RegisterVCenter(onPremReg *models.VCenterRegistration) err return nil } +func (m *Marketplace) Subscribe(request *models.OnPremSubscriptionRequest) error { + res, err := m.AgentOperations.SubscribeSolutions(request) + if err != nil { + return err + } + + log.Println(fmt.Sprintf("Subscription successful %v", res.SubscriptionDetails)) + return nil +} + func (m *Marketplace) GetActiveOnPremReg(agentID string) ([]*models.OnPremRegistrationDetail, error) { registrationDtls := make([]*models.OnPremRegistrationDetail, 0) diff --git a/pkg/agent_operations.go b/pkg/agent_operations.go index 31c4036..ac455c4 100644 --- a/pkg/agent_operations.go +++ b/pkg/agent_operations.go @@ -20,14 +20,14 @@ type AgentOperations interface { AgentLogin(agentLoginReq *models.AgentLoginRequest) (*models.AgentLoginResponseRest, error) RegisterVCenter(registrationReq *models.VCenterRegistration) (*models.VCenterRegistrationResponse, error) UpdateOnPremDeploymentStatus(statusUpdaterReq *agnt.DeploymentStatusRequest) error + SubscribeSolutions(subscriptionReq *models.OnPremSubscriptionRequest) (*models.OnPremSubscriptionResponse, error) } type agentOperationsHandler struct { vctrClient clients.VCenter Host string Client HTTPClient - //vcenterRegistrationCredentials *models.RegisteredVCenterCredentials - config *agent.Config + config *agent.Config } func (a agentOperationsHandler) UpdateOnPremDeploymentStatus(statusUpdaterReq *agnt.DeploymentStatusRequest) error { @@ -104,6 +104,48 @@ func (a agentOperationsHandler) RegisterVCenter(registrationReq *models.VCenterR } +func (a agentOperationsHandler) SubscribeSolutions(subscriptionReq *models.OnPremSubscriptionRequest) (*models.OnPremSubscriptionResponse, error) { + log.Println("@ subscription rest operation initiated...") + subRes := &models.OnPremSubscriptionResponse{} + requestURL := MakeURL( + a.Host, + fmt.Sprintf("/api/v1/on-prem/agent/subscriptions"), + url.Values{}, + ) + + log.Println("@@ subscription Req On prem Reg ID :@@ ", subscriptionReq.VCenterRegistrationID) + + resp, err := a.Client.PostJSON(requestURL, subscriptionReq) + if err != nil { + log.Println("failed to subscribe the product with error", err) + + return subRes, fmt.Errorf("product subscription failed") + } + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return subRes, fmt.Errorf("failed to subscribe product %s : (%d)", subscriptionReq, resp.StatusCode) + } + + log.Println("response body :", body) + + return subRes, fmt.Errorf("failed to subscribe product %s : (%d)", subscriptionReq, resp.StatusCode) + } + + err = DecodeJson(resp.Body, subRes) + if err != nil { + log.Println("failed to decode agent login response with error: ", err) + + return nil, fmt.Errorf("failed to parse the response for product subscription response %s: %w", subRes, err) + } + + log.Println("@@@@@@ Successfully subscribe the product @@@@@@@@@@") + + return subRes, err + +} + func (a agentOperationsHandler) AgentLogin(agentLoginReq *models.AgentLoginRequest) (*models.AgentLoginResponseRest, error) { agentLoginRes := &models.AgentLoginResponseRest{} log.Println("@@@@@@ Agent login initiated @@@@@@@@@@") diff --git a/pkg/http_client.go b/pkg/http_client.go index 3ede685..a186b86 100644 --- a/pkg/http_client.go +++ b/pkg/http_client.go @@ -179,7 +179,7 @@ func MakeURL(host, path string, values url.Values) *url.URL { queryString = values.Encode() } return &url.URL{ - Scheme: "https", + Scheme: "http", Host: host, Path: path, RawQuery: queryString, diff --git a/pkg/marketplace.go b/pkg/marketplace.go index f5aa847..88b40b4 100644 --- a/pkg/marketplace.go +++ b/pkg/marketplace.go @@ -55,12 +55,12 @@ type MarketplaceInterface interface { // Agent processes: RegisterVCenter(onPremReg *models.VCenterRegistration) error - //AgentLogin(agentLoginReq *models.AgentLoginRequest) (*models.AgentLoginResponseRest, error) AgentLogin() error //AgentHealth(agentHealthReq *models.AgentHealthRequest) error UpdateOnPremDeploymentStatus(statusUpdaterReq *agentmodels.DeploymentStatusRequest) error GetActiveOnPremReg(agentID string) ([]*models.OnPremRegistrationDetail, error) PollActiveOnPremDeployments(agentID, vCenterID string) (*models.OnPremSubscription, error) + Subscribe(statusUpdaterReq *models.OnPremSubscriptionRequest) error } type Marketplace struct {