Skip to content

Commit

Permalink
Add endpoint to enable shared cluster (#73)
Browse files Browse the repository at this point in the history
Disable endpoint was present but enable is missing.
This change fixes that.
  • Loading branch information
fridim authored Jul 1, 2024
1 parent 8dba136 commit afcead4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/sandbox-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func main() {
r.Get("/api/v1/ocp-shared-cluster-configurations", baseHandler.GetOcpSharedClusterConfigurationsHandler)
r.Get("/api/v1/ocp-shared-cluster-configurations/{name}", baseHandler.GetOcpSharedClusterConfigurationHandler)
r.Put("/api/v1/ocp-shared-cluster-configurations/{name}/disable", baseHandler.DisableOcpSharedClusterConfigurationHandler)
r.Put("/api/v1/ocp-shared-cluster-configurations/{name}/enable", baseHandler.EnableOcpSharedClusterConfigurationHandler)
r.Delete("/api/v1/ocp-shared-cluster-configurations/{name}", baseHandler.DeleteOcpSharedClusterConfigurationHandler)

// Reservations
Expand Down
42 changes: 42 additions & 0 deletions cmd/sandbox-api/ocp_shared_cluster_configuration_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,48 @@ func (h *BaseHandler) DisableOcpSharedClusterConfigurationHandler(w http.Respons
})
}

func (h *BaseHandler) EnableOcpSharedClusterConfigurationHandler(w http.ResponseWriter, r *http.Request) {
// Get the name of the OCP shared cluster configuration from the URL
name := chi.URLParam(r, "name")

// Get the OCP shared cluster configuration from the database
ocpSharedClusterConfiguration, err := h.OcpSandboxProvider.GetOcpSharedClusterConfigurationByName(name)
if err != nil {
if err == pgx.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
render.Render(w, r, &v1.Error{
HTTPStatusCode: http.StatusNotFound,
Message: "OCP shared cluster configuration not found",
})
return
}

w.WriteHeader(http.StatusInternalServerError)
render.Render(w, r, &v1.Error{
HTTPStatusCode: http.StatusInternalServerError,
Message: "Failed to get OCP shared cluster configuration",
ErrorMultiline: []string{err.Error()},
})
return
}

// Enable the OCP shared cluster configuration
if err := ocpSharedClusterConfiguration.Enable(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
render.Render(w, r, &v1.Error{
HTTPStatusCode: http.StatusInternalServerError,
Message: "Failed to enable OCP shared cluster configuration",
ErrorMultiline: []string{err.Error()},
})
return
}

w.WriteHeader(http.StatusOK)
render.Render(w, r, &v1.SimpleMessage{
Message: "OCP shared cluster configuration is enabled",
})
}

// GetOcpSharedClusterConfigurationsHandlers returns a list of OCP shared cluster configurations
func (h *BaseHandler) GetOcpSharedClusterConfigurationsHandler(w http.ResponseWriter, r *http.Request) {
ocpSharedClusterConfigurations, err := h.OcpSandboxProvider.GetOcpSharedClusterConfigurations()
Expand Down
35 changes: 35 additions & 0 deletions docs/api-reference/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,41 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
/ocp-shared-cluster-configurations/{name}/enable:
put:
summary: Enable an OcpSharedClusterConfiguration
operationId: enableOcpSharedClusterConfiguration
tags:
- admin
parameters:
- name: name
in: path
required: true
description: The name of the OcpSharedClusterConfiguration
schema:
type: string
example: ocp-cluster-1
responses:
'200':
description: The OcpSharedClusterConfiguration is enabled
content:
application/json:
schema:
$ref: "#/components/schemas/Message"
example:
message: OCP shared cluster configuration enabled
'404':
description: enableOcpSharedClusterConfiguration OcpSharedClusterConfiguration not found
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
default:
description: enableOcpSharedClusterConfiguration unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/ocp-shared-cluster-configurations/{name}/disable:
put:
summary: Disable an OcpSharedClusterConfiguration
Expand Down
6 changes: 6 additions & 0 deletions internal/models/ocp_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ func (p *OcpSharedClusterConfiguration) Disable() error {
return p.Update()
}

// Enable an OcpSharedClusterConfiguration
func (p *OcpSharedClusterConfiguration) Enable() error {
p.Valid = true
return p.Update()
}

// CountAccounts returns the number of accounts for an OcpSharedClusterConfiguration
func (p *OcpSharedClusterConfiguration) GetAccountCount() (int, error) {
var count int
Expand Down
15 changes: 15 additions & 0 deletions tests/002_ocp.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ jsonpath "$.name" == "ocp-cluster-test1"
jsonpath "$.annotations.virt" == "no"
jsonpath "$.valid" == false

# Enable cluster
PUT {{host}}/api/v1/ocp-shared-cluster-configurations/ocp-cluster-test1/enable
Authorization: Bearer {{ access_token_admin }}
HTTP 200
[Asserts]
jsonpath "$.message" == "OCP shared cluster configuration is enabled"

GET {{host}}/api/v1/ocp-shared-cluster-configurations/ocp-cluster-test1
Authorization: Bearer {{access_token_admin}}
HTTP 200
[Asserts]
jsonpath "$.name" == "ocp-cluster-test1"
jsonpath "$.valid" == true


DELETE {{host}}/api/v1/ocp-shared-cluster-configurations/ocp-cluster-test1
Authorization: Bearer {{access_token_admin}}
[Options]
Expand Down

0 comments on commit afcead4

Please sign in to comment.