Skip to content

Commit

Permalink
Passthrough client creation error. Remove duplicated import.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuolun-citrix committed Oct 27, 2023
1 parent 304d52f commit 15d71c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
36 changes: 18 additions & 18 deletions client/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package citrixclient

import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -30,48 +29,49 @@ type TrustAuthResponse struct {
}

// SignIn - Get a new token for user
func (c *CitrixDaasClient) SignIn() (string, error) {
func (c *CitrixDaasClient) SignIn() (string, *http.Response, error) {
if c.AuthConfig.ClientId == "" || c.AuthConfig.ClientSecret == "" {
return "", fmt.Errorf("make sure customerid, clientid and clientsecret are not null")
return "", nil, fmt.Errorf("make sure customerid, clientid and clientsecret are not null")
}

if c.AuthToken != nil {
tokenExpirationTime, err := time.Parse(time.RFC3339, c.AuthToken.ExpiresAt)

if err == nil && tokenExpirationTime.After(time.Now().UTC().Add(time.Minute*time.Duration(1))) {
return c.AuthToken.Token, nil
return c.AuthToken.Token, nil, nil
}
}

var token string
var expiresAt string

if c.AuthConfig.OnPremise {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
// tr := &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// }
// client := &http.Client{Transport: tr}
client := c.ApiClient.GetConfig().HTTPClient
req, _ := http.NewRequest("POST", c.AuthConfig.AuthUrl, nil)
req.SetBasicAuth(c.AuthConfig.ClientId, c.AuthConfig.ClientSecret)
resp, err := client.Do(req)
if err != nil {
return "", err
return "", nil, err
}

defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return "", nil, err
}

if resp.StatusCode > 200 {
return "", fmt.Errorf("Could not sign into DDC, %s", string(body))
return "", resp, fmt.Errorf("could not sign into DDC, %s", string(body))
}

ar := TrustAuthResponse{}
err = json.Unmarshal(body, &ar)
if err != nil {
return "", err
return "", nil, err
}

token = fmt.Sprintf("CWSAuth bearer=%s", ar.Token)
Expand All @@ -81,27 +81,27 @@ func (c *CitrixDaasClient) SignIn() (string, error) {
grant_type := "client_credentials"
resp, err := http.PostForm(c.AuthConfig.AuthUrl, url.Values{"grant_type": {grant_type}, "client_id": {c.AuthConfig.ClientId}, "client_secret": {c.AuthConfig.ClientSecret}})
if err != nil {
return "", err
return "", nil, err
}

defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return "", nil, err
}

if resp.StatusCode > 200 {
return "", fmt.Errorf("Could not sign into Citrix Cloud, %s", string(body))
return "", resp, fmt.Errorf("could not sign into Citrix Cloud, %s", string(body))
}

ar := CCAuthResponse{}
err = json.Unmarshal(body, &ar)
if err != nil {
return "", err
return "", nil, err
}

if ar.Error != "" {
return "", fmt.Errorf("Could not sign into Citrix Cloud, %s: %s", ar.Error, ar.ErrorDescription)
return "", nil, fmt.Errorf("could not sign into Citrix Cloud, %s: %s", ar.Error, ar.ErrorDescription)
}

token = fmt.Sprintf("CWSAuth bearer=%s", ar.Token)
Expand All @@ -115,5 +115,5 @@ func (c *CitrixDaasClient) SignIn() (string, error) {
authTokenModel.ExpiresAt = expiresAt
c.AuthToken = &authTokenModel

return token, nil
return token, nil, nil
}
27 changes: 13 additions & 14 deletions client/citrixdaas_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"time"

"github.com/citrix/citrix-daas-rest-go/citrixorchestration"
openapiclient "github.com/citrix/citrix-daas-rest-go/citrixorchestration"
)

type CitrixDaasClient struct {
ApiClient *openapiclient.APIClient
ApiClient *citrixorchestration.APIClient
AuthConfig *AuthenticationConfiguration
ClientConfig *ClientConfiguration
AuthToken *AuthTokenModel
Expand All @@ -32,11 +31,11 @@ func getMiddlewareWithClient(authClient *CitrixDaasClient, middlewareAuthFunc Mi
}
}

func NewCitrixDaasClient(authUrl, hostname, customerId, clientId, clientSecret string, onPremise bool, disableSslVerification bool, userAgent *string, middlewareFunc MiddlewareAuthFunction) (*CitrixDaasClient, error) {
func NewCitrixDaasClient(authUrl, hostname, customerId, clientId, clientSecret string, onPremise bool, disableSslVerification bool, userAgent *string, middlewareFunc MiddlewareAuthFunction) (*CitrixDaasClient, *http.Response, error) {
daasClient := &CitrixDaasClient{}

/* ------ Setup API Client ------ */
localCfg := openapiclient.NewConfiguration()
localCfg := citrixorchestration.NewConfiguration()
localCfg.Host = hostname
localCfg.Scheme = "https"
// When running against on-prem, set disableSslVerification to true when the DDC does not have a valid TLS/SSL certificate
Expand All @@ -47,14 +46,14 @@ func NewCitrixDaasClient(authUrl, hostname, customerId, clientId, clientSecret s
client := &http.Client{Transport: tr}
localCfg.HTTPClient = client

localCfg.Servers = openapiclient.ServerConfigurations{
localCfg.Servers = citrixorchestration.ServerConfigurations{
{
URL: localCfg.Scheme + "://" + hostname + "/citrix/orchestration/api/techpreview",
},
}
}

daasClient.ApiClient = openapiclient.NewAPIClient(localCfg)
daasClient.ApiClient = citrixorchestration.NewAPIClient(localCfg)
localCfg.Middleware = getMiddlewareWithClient(daasClient, middlewareFunc)

/* ------ Setup Authentication Configuration ------ */
Expand All @@ -68,14 +67,14 @@ func NewCitrixDaasClient(authUrl, hostname, customerId, clientId, clientSecret s

/* ------ Setup Client Configuration ------*/
req := daasClient.ApiClient.MeAPIsDAAS.MeGetMe(context.Background())
token, err := daasClient.SignIn()
token, httpResp, err := daasClient.SignIn()
if err != nil {
return nil, err
return nil, httpResp, err
}
req = req.Authorization(token).CitrixCustomerId(customerId)
resp, _, err := req.Execute()
resp, httpResp, err := req.Execute()
if err != nil {
return nil, err
return nil, httpResp, err
}

localClientCfg := &ClientConfiguration{}
Expand All @@ -95,14 +94,14 @@ func NewCitrixDaasClient(authUrl, hostname, customerId, clientId, clientSecret s
localCfg.Servers[0].URL += "/" + localClientCfg.CustomerId + "/" + localClientCfg.SiteId
}

return daasClient, nil
return daasClient, httpResp, nil
}

func (c *CitrixDaasClient) WaitForJob(ctx context.Context, jobId string, maxWaitTimeInMinutes int) (*openapiclient.JobResponseModel, error) {
func (c *CitrixDaasClient) WaitForJob(ctx context.Context, jobId string, maxWaitTimeInMinutes int) (*citrixorchestration.JobResponseModel, error) {

maxWaitTime := time.Now().UTC().Add(time.Minute * time.Duration(maxWaitTimeInMinutes))
getJobIdRequest := c.ApiClient.JobsAPIsDAAS.JobsGetJob(ctx, jobId)
var jobResponseModel *openapiclient.JobResponseModel
var jobResponseModel *citrixorchestration.JobResponseModel
var err error

for {
Expand All @@ -115,7 +114,7 @@ func (c *CitrixDaasClient) WaitForJob(ctx context.Context, jobId string, maxWait
return jobResponseModel, err
}

if jobResponseModel.Status == openapiclient.JOBSTATUS_UNKNOWN || jobResponseModel.Status == openapiclient.JOBSTATUS_NOT_STARTED || jobResponseModel.Status == openapiclient.JOBSTATUS_IN_PROGRESS {
if jobResponseModel.Status == citrixorchestration.JOBSTATUS_UNKNOWN || jobResponseModel.Status == citrixorchestration.JOBSTATUS_NOT_STARTED || jobResponseModel.Status == citrixorchestration.JOBSTATUS_IN_PROGRESS {
time.Sleep(time.Second * time.Duration(30))
continue
}
Expand Down

0 comments on commit 15d71c9

Please sign in to comment.