Skip to content

Commit

Permalink
feat: add subscription to image model to init the support for activat…
Browse files Browse the repository at this point in the history
…ion key on edge-api

Signed-off-by: Adelia Ferreira <adferrei@redhat.com>
  • Loading branch information
adeliaferreira committed Jan 18, 2024
1 parent 307a4db commit bae9f05
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type EdgeConfig struct {
DeleteFilesRetryDelay uint `json:"delete_files_retry_delay"`
RbacBaseURL string `json:"rbac_base_url"`
RbacTimeout uint `mapstructure:"rbac_timeout,omitempty"`
SubscriptionServerURL string `json:"subscription_server_url"`
SubscriptionBaseUrl string `json:"subscription_base_url"`
}

type dbConfig struct {
Expand Down Expand Up @@ -275,6 +277,8 @@ func CreateEdgeAPIConfig() (*EdgeConfig, error) {
DeleteFilesRetryDelay: options.GetUint("DeleteFilesRetryDelay"),
RbacBaseURL: options.GetString("RBAC_BASE_URL"),
RbacTimeout: options.GetUint("RbacTimeout"),
SubscriptionServerURL: options.GetString("SUBSCRIPTION_SERVER_URL"),
SubscriptionBaseUrl: options.GetString("SUBSCRIPTION_BASE_URL"),
}
if edgeConfig.TenantTranslatorHost != "" && edgeConfig.TenantTranslatorPort != "" {
edgeConfig.TenantTranslatorURL = fmt.Sprintf("http://%s:%s", edgeConfig.TenantTranslatorHost, edgeConfig.TenantTranslatorPort)
Expand Down
38 changes: 38 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,44 @@ func TestContentSourcesURL(t *testing.T) {
assert.Equal(t, expectedContentSourcesURl, conf.ContentSourcesURL)
}

func TestSubscriptionBaseURL(t *testing.T) {
subscriptionBaseURLEnvName := "SUBSCRIPTION_BASE_URL"
initialSubscriptionBaseURLEnv := os.Getenv(subscriptionBaseURLEnvName)

// restore initial content source env value
defer func(envName, envValue string) {
err := os.Setenv(envName, envValue)
assert.NoError(t, err)
}(subscriptionBaseURLEnvName, initialSubscriptionBaseURLEnv)

expectedSubscriptionBaseURL := faker.URL()
err := os.Setenv(subscriptionBaseURLEnvName, expectedSubscriptionBaseURL)
assert.NoError(t, err)

conf, err := CreateEdgeAPIConfig()
assert.NoError(t, err)
assert.Equal(t, expectedSubscriptionBaseURL, conf.SubscriptionBaseUrl)
}

func TestSubscriptionServerURL(t *testing.T) {
subscriptionServerURLEnvName := "SUBSCRIPTION_SERVER_URL"
initialSubscriptionServerURLEnv := os.Getenv(subscriptionServerURLEnvName)

// restore initial content source env value
defer func(envName, envValue string) {
err := os.Setenv(envName, envValue)
assert.NoError(t, err)
}(subscriptionServerURLEnvName, initialSubscriptionServerURLEnv)

expectedSubscriptionServerURL := faker.URL()
err := os.Setenv(subscriptionServerURLEnvName, expectedSubscriptionServerURL)
assert.NoError(t, err)

conf, err := CreateEdgeAPIConfig()
assert.NoError(t, err)
assert.Equal(t, expectedSubscriptionServerURL, conf.SubscriptionServerURL)
}

func TestTLSCAPath(t *testing.T) {
// restore initial clowder config
defer func(clowderLoadedConfig *clowder.AppConfig) {
Expand Down
28 changes: 28 additions & 0 deletions deploy/clowdapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ objects:
value: ${TENANT_TRANSLATOR_PORT}
- name : RBAC_BASE_URL
value: ${RBAC_BASE_URL}
- name : SUBSCRIPTION_BASE_URL
value: ${SUBSCRIPTION_BASE_URL}
- name : SUBSCRIPTION_SERVER_URL
value: ${SUBSCRIPTION_SERVER_URL}
resources:
limits:
cpu: ${{CPU_LIMIT}}
Expand Down Expand Up @@ -202,6 +206,10 @@ objects:
value: ${TENANT_TRANSLATOR_PORT}
- name : RBAC_BASE_URL
value: ${RBAC_BASE_URL}
- name : SUBSCRIPTION_BASE_URL
value: ${SUBSCRIPTION_BASE_URL}
- name : SUBSCRIPTION_SERVER_URL
value: ${SUBSCRIPTION_SERVER_URL}
resources:
limits:
cpu: 250m
Expand Down Expand Up @@ -269,6 +277,10 @@ objects:
value: ${TENANT_TRANSLATOR_PORT}
- name : RBAC_BASE_URL
value: ${RBAC_BASE_URL}
- name : SUBSCRIPTION_BASE_URL
value: ${SUBSCRIPTION_BASE_URL}
- name : SUBSCRIPTION_SERVER_URL
value: ${SUBSCRIPTION_SERVER_URL}
resources:
limits:
cpu: 250m
Expand Down Expand Up @@ -336,6 +348,10 @@ objects:
value: ${TENANT_TRANSLATOR_PORT}
- name : RBAC_BASE_URL
value: ${RBAC_BASE_URL}
- name : SUBSCRIPTION_BASE_URL
value: ${SUBSCRIPTION_BASE_URL}
- name : SUBSCRIPTION_SERVER_URL
value: ${SUBSCRIPTION_SERVER_URL}
resources:
limits:
cpu: 250m
Expand Down Expand Up @@ -407,6 +423,10 @@ objects:
value: ${TENANT_TRANSLATOR_PORT}
- name : RBAC_BASE_URL
value: ${RBAC_BASE_URL}
- name : SUBSCRIPTION_BASE_URL
value: ${SUBSCRIPTION_BASE_URL}
- name : SUBSCRIPTION_SERVER_URL
value: ${SUBSCRIPTION_SERVER_URL}
resources:
limits:
cpu: 900m
Expand Down Expand Up @@ -556,3 +576,11 @@ parameters:
name: RBAC_BASE_URL
required: false
value: "http://rbac-service:8080"
- description: SUBSCRIPTION service base URL
name: SUBSCRIPTION_BASE_URL
required: false
value: "http://subscription-service:8080"
- description: SUBSCRIPTION server URL
name: SUBSCRIPTION_SERVER_URL
required: false
value: "http://subscription-servers:8080"
19 changes: 19 additions & 0 deletions pkg/clients/imagebuilder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Customizations struct {
Packages *[]string `json:"packages"`
PayloadRepositories *[]Repository `json:"payload_repositories,omitempty"`
Users *[]User `json:"users,omitempty"`
Subscription *Subscription `json:"subscription,omitempty"`
}

// Repository is the record of Third Party Repository
Expand All @@ -88,6 +89,14 @@ type User struct {
Name string `json:"name"`
SSHKey string `json:"ssh_key"`
}
type Subscription struct {
Organization string `json:"organization"`
ActivationKey string `json:"activation-key"`
BaseUrl string `json:"base-url"`
ServerUrl string `json:"server-url"`
Insights bool `json:"insights"`
RHC bool `json:"rhc"`
}

// UploadRequest is the upload options accepted by Image Builder API
type UploadRequest struct {
Expand Down Expand Up @@ -248,6 +257,16 @@ func (c *Client) ComposeCommit(image *models.Image) (*models.Image, error) {
},
}},
}
if image.ActivationKey != "" {
req.Customizations.Subscription = &Subscription{
ActivationKey: image.ActivationKey,
Organization: image.OrgID,
BaseUrl: config.Get().SubscriptionBaseUrl,
ServerUrl: config.Get().SubscriptionServerURL,
Insights: true,
RHC: true,
}
}
if image.Commit.OSTreeRef != "" {
if req.ImageRequests[0].Ostree == nil {
req.ImageRequests[0].Ostree = &OSTree{}
Expand Down
81 changes: 81 additions & 0 deletions pkg/clients/imagebuilder/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,87 @@ var _ = Describe("Image Builder Client Test", func() {
Expect(img.Commit.ComposeJobID).To(Equal("compose-job-id-returned-from-image-builder"))
Expect(img.Commit.ExternalURL).To(BeFalse())
})
It("image actication key is filled", func() {
composeJobID := faker.UUIDHyphenated()
orgID := faker.UUIDHyphenated()
repoURL := fmt.Sprintf("%s/api/edge/v1/storage/images-repos/12345", config.Get().EdgeCertAPIBaseURL)
dist := "rhel-84"
newDist := "rhel-85"

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req ComposeRequest
body, err := io.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
Expect(body).ToNot(BeNil())
err = json.Unmarshal(body, &req)
Expect(err).ToNot(HaveOccurred())
Expect(req.Customizations.Subscription.ActivationKey).To(Equal("test-key"))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_, err = fmt.Fprintf(w, `{"id": "%s"}`, composeJobID)
Expect(err).ToNot(HaveOccurred())
}))
defer ts.Close()
config.Get().ImageBuilderConfig.URL = ts.URL

img := &models.Image{Distribution: dist,
Name: faker.Name(),
OrgID: orgID,
Commit: &models.Commit{
OrgID: orgID,
Arch: "x86_64",
Repo: &models.Repo{},
OSTreeRef: config.DistributionsRefs[newDist],
OSTreeParentRef: config.DistributionsRefs[dist],
OSTreeParentCommit: repoURL,
},
ActivationKey: "test-key",
}

img, err := client.ComposeCommit(img)
Expect(err).ToNot(HaveOccurred())
Expect(img).ToNot(BeNil())
Expect(img.Commit.ComposeJobID).To(Equal(composeJobID))
Expect(img.Commit.ExternalURL).To(BeFalse())
})

It("image actication key is not filled", func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req ComposeRequest
body, err := io.ReadAll(r.Body)
Expect(err).ToNot(HaveOccurred())
Expect(body).ToNot(BeNil())
err = json.Unmarshal(body, &req)
Expect(err).ToNot(HaveOccurred())
Expect(req.Customizations.Subscription).To(BeNil())
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintln(w, `{"id": "compose-job-id-returned-from-image-builder"}`)
}))
defer ts.Close()
config.Get().ImageBuilderConfig.URL = ts.URL

pkgs := []models.Package{
{
Name: "vim",
},
{
Name: "ansible",
},
}
img := &models.Image{Distribution: "rhel-8",
Packages: pkgs,
Commit: &models.Commit{
Arch: "x86_64",
Repo: &models.Repo{},
}}
img, err := client.ComposeCommit(img)
Expect(err).ToNot(HaveOccurred())
Expect(img).ToNot(BeNil())
Expect(img.Commit.ComposeJobID).To(Equal("compose-job-id-returned-from-image-builder"))
Expect(img.Commit.ExternalURL).To(BeFalse())
})

It("should return error when image has org_id undefined", func() {
pkgs := []models.Package{
{
Expand Down
1 change: 1 addition & 0 deletions pkg/models/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Image struct {
ThirdPartyRepositories []ThirdPartyRepo `json:"ThirdPartyRepositories,omitempty" gorm:"many2many:images_repos;"`
CustomPackages []Package `json:"CustomPackages,omitempty" gorm:"many2many:images_custom_packages"`
RequestID string `json:"request_id"` // storing for logging reference on resume
ActivationKey string `json:"activationKey,omitempty"`

TotalDevicesWithImage int64 `json:"SystemsRunning" gorm:"-"` // only for forms
TotalPackages int `json:"TotalPackages" gorm:"-"` // only for forms
Expand Down

0 comments on commit bae9f05

Please sign in to comment.