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

Download flow changed- replicated #57

Merged
merged 2 commits into from
Jul 26, 2024
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
8 changes: 8 additions & 0 deletions clients/iLicense.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package clients

type ILicense interface {
Get(url string) *Request
Validate(id, licenseServiceUrl string, data *Response) *Request
GetReplicatedCustomerEmail(licenseId, licenseServiceUrl string, data *Response) *Request
IsTrial(l string) bool
}
17 changes: 15 additions & 2 deletions clients/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ import (
"github.com/gofiber/fiber/v2"
)

type GetReplicatedCustomerResponse struct {
ReplicatedEmail string `json:"replicatedEmail"`
Message string `json:"message"`
StatusCode string `json:"status_code"`
}
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type License struct {
client *http.Client
client HTTPClient
}

type RequestParams struct {
Expand All @@ -25,7 +33,7 @@ type Response struct {
Code int //json: "status_code"
}

func NewLicenseClient() *License {
func NewLicenseClient() ILicense {
return &License{
client: &http.Client{
Timeout: 10 * time.Second,
Expand Down Expand Up @@ -72,6 +80,11 @@ func (c *License) Validate(id, licenseServiceUrl string, data *Response) *Reques
return c.Get(url).ParseLicenseResp(&data)
}

func (c *License) GetReplicatedCustomerEmail(licenseId, licenseServiceUrl string, data *Response) *Request {
requestUrl := fmt.Sprintf("%s/v1/getReplicatedCustomer?licenseId=%s", licenseServiceUrl, licenseId)
return c.Get(requestUrl).ParseLicenseResp(&data)
}

func (c *License) IsTrial(l string) bool {
return strings.Contains(l, "tmns")
}
49 changes: 49 additions & 0 deletions clients/license.mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package clients

import (
"net/http"
)

type MockLicense struct {
GetFunc func(url string) *Request
ValidateFunc func(id, licenseServiceUrl string, data *Response) *Request
GetReplicatedCustomerEmailFunc func(licenseId, licenseServiceUrl string, data *Response) *Request
IsTrialFunc func(l string) bool
}

type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}

func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
if m.DoFunc != nil {
return m.DoFunc(req)
}
return &http.Response{}, nil
}

func NewMockLicenseClient() ILicense {
return &License{
client: &MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{}, nil
},
},
}
}

func (m *MockLicense) GetReplicatedCustomerEmail(licenseId, licenseServiceUrl string, data *Response) *Request {
return m.GetReplicatedCustomerEmailFunc(licenseId, licenseServiceUrl, data)
}

func (m *MockLicense) Get(url string) *Request {
return m.GetFunc(url)
}

func (m *MockLicense) Validate(id, licenseServiceUrl string, data *Response) *Request {
return m.ValidateFunc(id, licenseServiceUrl, data)
}

func (m *MockLicense) IsTrial(l string) bool {
return m.IsTrialFunc(l)
}
6 changes: 6 additions & 0 deletions clients/omnitruck/replicated/replicated.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package replicated

import (
"net/http"
)

type IReplicated interface {
SearchCustomersByEmail(email string, requestId string) (customers []Customer, err error)
GetDowloadUrl(customer Customer, requestId string) (url string, err error)
DownloadFromReplicated(url, requestId, authorization string) (res *http.Response, err error)
}
37 changes: 37 additions & 0 deletions clients/omnitruck/replicated/replicated.impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ func (r ReplicatedImpl) makeRequest(url, method, requestId string, payload io.Re
return res.StatusCode, body, nil
}

func (r ReplicatedImpl) DownloadFromReplicated(url, requestId, authorization string) (res *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", authorization)

// Perform the request
resp, err := r.Client.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}

func (r ReplicatedImpl) SearchCustomersByEmail(email string, requestId string) (customers []Customer, err error) {
log := utils.AddLogFields("SearchCustomersByEmail", requestId, r.Logger)

Expand Down Expand Up @@ -105,3 +121,24 @@ func (r ReplicatedImpl) SearchCustomersByEmail(email string, requestId string) (

return respObj.Customers, nil
}

func (r *ReplicatedImpl) GetDowloadUrl(customer Customer, requestId string) (url string, err error) {
log := utils.AddLogFields("GetDowloadUrl", requestId, r.Logger)
if len(customer.Channels) == 0 {
log.Error("No channel found for download ")
return "", fmt.Errorf("no channels found for customer %s", customer.ID)
}

channel := customer.Channels[0]

if channel.AppSlug == "" || channel.ChannelSlug == "" {
log.Error("Empty app or channel slug")
return "", fmt.Errorf("empty app or channel slug found for customer %s", customer.ID)
}
url = constants.REPLICATED_DOWNLOAD_URL + "/" + channel.AppSlug + "/" + channel.ChannelSlug

if customer.Airgap {
url += "?airgap=true"
}
return url, nil
}
184 changes: 184 additions & 0 deletions clients/omnitruck/replicated/replicated.impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/chef/omnitruck-service/clients/omnitruck/replicated"
"github.com/chef/omnitruck-service/config"
"github.com/chef/omnitruck-service/constants"
"github.com/chef/omnitruck-service/logger"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -86,3 +87,186 @@ func TestSearchCustomerByEmail500(t *testing.T) {
_, err := repImp.SearchCustomersByEmail("[email protected]", "")
assert.Error(t, err)
}

func TestReplicatedImpl_GetDowloadUrl(t *testing.T) {
type fields struct {
ReplicatedConfig config.ReplicatedConfig
Client replicated.HTTPClient
Logger logger.Logger
}
type args struct {
customer replicated.Customer
requestId string
}
tests := []struct {
name string
fields fields
args args
wantUrl string
wantErr bool
}{
{
name: "No channel error",
fields: fields{
ReplicatedConfig: config.ReplicatedConfig{},
Logger: logger.NewLogrusStandardLogger(),
},
args: args{customer: replicated.Customer{
ID: "cust123",
Airgap: true,
Channels: []replicated.Channel{},
}},
wantUrl: "",
wantErr: true,
},
{
name: "Empty slug value",
fields: fields{
ReplicatedConfig: config.ReplicatedConfig{},
Logger: logger.NewLogrusStandardLogger(),
},
args: args{customer: replicated.Customer{
ID: "cust123",
Airgap: true,
Channels: []replicated.Channel{
{ID: "channel123", AppSlug: "app123"},
},
}},
wantUrl: "",
wantErr: true,
},
{
name: "Non Airgap",
fields: fields{
ReplicatedConfig: config.ReplicatedConfig{},
Logger: logger.NewLogrusStandardLogger(),
},
args: args{customer: replicated.Customer{
ID: "cust123",
Airgap: false,
Channels: []replicated.Channel{
{ID: "channel123", AppSlug: "app123", ChannelSlug: "ch123"},
},
}},
wantUrl: constants.REPLICATED_DOWNLOAD_URL + "/app123/ch123",
wantErr: false,
},
{
name: "Airgap",
fields: fields{
ReplicatedConfig: config.ReplicatedConfig{},
Logger: logger.NewLogrusStandardLogger(),
},
args: args{customer: replicated.Customer{
ID: "cust123",
Airgap: true,
Channels: []replicated.Channel{
{ID: "channel123", AppSlug: "app123", ChannelSlug: "ch123"},
},
}},
wantUrl: constants.REPLICATED_DOWNLOAD_URL + "/app123/ch123?airgap=true",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &replicated.ReplicatedImpl{
ReplicatedConfig: tt.fields.ReplicatedConfig,
Client: tt.fields.Client,
Logger: tt.fields.Logger,
}
gotUrl, err := r.GetDowloadUrl(tt.args.customer, tt.args.requestId)
if tt.wantErr {
assert.NotNil(t, err)
assert.Empty(t, gotUrl)
} else {
assert.NotEmpty(t, gotUrl)
assert.Equal(t, tt.wantUrl, gotUrl)
assert.Nil(t, err)
}

})
}
}

func TestReplicatedImpl_DownloadFromReplicated(t *testing.T) {
type fields struct {
ReplicatedConfig config.ReplicatedConfig
Client replicated.HTTPClient
Logger logger.Logger
}
type args struct {
url string
requestid string
authorization string
}
tests := []struct {
name string
fields fields
args args
wantRes *http.Response
wantErr bool
}{
{
name: "Success",
fields: fields{
Client: &MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
responseBody := io.NopCloser(bytes.NewReader([]byte(`{"message": "success"}`)))
return &http.Response{
StatusCode: 200,
Body: responseBody,
}, nil
},
},
ReplicatedConfig: config.ReplicatedConfig{},
Logger: logger.NewLogrusStandardLogger(),
},
args: args{
url: "https://www.example.com",
requestid: "req123",
authorization: "123",
},
wantRes: &http.Response{StatusCode: 200},
wantErr: false,
},
{
name: "Error",
fields: fields{
Client: &MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("Error")
},
},
ReplicatedConfig: config.ReplicatedConfig{},
Logger: logger.NewLogrusStandardLogger(),
},
args: args{
url: "https://www.example.com",
requestid: "req123",
authorization: "123",
},
wantRes: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := replicated.ReplicatedImpl{
ReplicatedConfig: tt.fields.ReplicatedConfig,
Client: tt.fields.Client,
Logger: tt.fields.Logger,
}
gotRes, err := r.DownloadFromReplicated(tt.args.url, tt.args.requestid, tt.args.authorization)

if tt.wantErr {
assert.Nil(t, gotRes)
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tt.wantRes.StatusCode, gotRes.StatusCode)
}

})
}
}
12 changes: 12 additions & 0 deletions clients/omnitruck/replicated/replicated.mock.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package replicated

import "net/http"

type MockReplicated struct {
SearchCustomersByEmailFunc func(email string, requestId string) (customers []Customer, err error)
GetDowloadUrlFunc func(customer Customer, requestId string) (url string, err error)
DownloadFromReplicatedFunc func(url, requestId, authorization string) (res *http.Response, err error)
}

func (m MockReplicated) SearchCustomersByEmail(email string, requestId string) (customers []Customer, err error) {
return m.SearchCustomersByEmailFunc(email, requestId)
}

func (m MockReplicated) GetDowloadUrl(customer Customer, requestId string) (url string, err error) {
return m.GetDowloadUrlFunc(customer, requestId)
}

func (m MockReplicated) DownloadFromReplicated(url, requestId, authorization string) (res *http.Response, err error) {
return m.DownloadFromReplicatedFunc(url, requestId, authorization)
}
15 changes: 9 additions & 6 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package constants

const (
SKU_PARTITION_KEY = "bom"
PRODUCT_PARTITION_KEY = "product"
PRODUCT_SORT_KEY = "version"
AUTOMATE_PRODUCT = "automate"
HABITAT_PRODUCT = "habitat"
LATEST = "latest"
SKU_PARTITION_KEY = "bom"
PRODUCT_PARTITION_KEY = "product"
PRODUCT_SORT_KEY = "version"
AUTOMATE_PRODUCT = "automate"
HABITAT_PRODUCT = "habitat"
LATEST = "latest"
PLATFORM_SERVICE = "chef-360"
PLATFORM_ERROR = "chef-360 not available for the trial and opensource "
REPLICATED_DOWNLOAD_URL = "https://replicated.app/embedded"
)

const (
Expand Down
Loading
Loading