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

new command for subscribe #152

Draft
wants to merge 1 commit into
base: on_prem_automate
Choose a base branch
from
Draft
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
10 changes: 5 additions & 5 deletions background/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/background_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func init() {
AgentProcessCmd.AddCommand(StartProcessCmd)
AgentProcessCmd.AddCommand(StopProcessCmd)
AgentProcessCmd.AddCommand(RegisterVCenterCmd)
AgentProcessCmd.AddCommand(SubscribeCmd)
}

var AgentProcessCmd = &cobra.Command{
Expand Down Expand Up @@ -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): ")
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions internal/models/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
10 changes: 10 additions & 0 deletions pkg/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
46 changes: 44 additions & 2 deletions pkg/agent_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 @@@@@@@@@@")
Expand Down
2 changes: 1 addition & 1 deletion pkg/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down