2021-08-26 14:00:59 +00:00
package kubernetes
import (
"net/http"
"github.com/portainer/portainer/api/http/middlewares"
2023-09-01 22:27:02 +00:00
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response"
2021-08-26 14:00:59 +00:00
)
type namespacesToggleSystemPayload struct {
// Toggle the system state of this namespace to true or false
System bool ` example:"true" `
}
func ( payload * namespacesToggleSystemPayload ) Validate ( r * http . Request ) error {
return nil
}
// @id KubernetesNamespacesToggleSystem
// @summary Toggle the system state for a namespace
2024-10-01 01:15:51 +00:00
// @description Toggle the system state for a namespace
// @description **Access policy**: Administrator or environment administrator.
// @security ApiKeyAuth || jwt
2021-08-26 14:00:59 +00:00
// @tags kubernetes
// @accept json
2024-10-01 01:15:51 +00:00
// @param id path int true "Environment identifier"
2021-08-26 14:00:59 +00:00
// @param namespace path string true "Namespace name"
// @param body body namespacesToggleSystemPayload true "Update details"
2024-10-01 01:15:51 +00:00
// @success 204 "Success"
// @failure 400 "Invalid request payload, such as missing required fields or fields not meeting validation criteria."
// @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions."
// @failure 403 "Permission denied - the user is authenticated but does not have the necessary permissions to access the requested resource or perform the specified operation. Check your user roles and permissions."
// @failure 404 "Unable to find an environment with the specified identifier or unable to find the namespace to update."
// @failure 500 "Server error occurred while attempting to update the system state of the namespace."
2021-08-26 14:00:59 +00:00
// @router /kubernetes/{id}/namespaces/{namespace}/system [put]
func ( handler * Handler ) namespacesToggleSystem ( rw http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
endpoint , err := middlewares . FetchEndpoint ( r )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . NotFound ( "Unable to find an environment on request context" , err )
2021-08-26 14:00:59 +00:00
}
namespaceName , err := request . RetrieveRouteVariableValue ( r , "namespace" )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . BadRequest ( "Invalid namespace identifier route variable" , err )
2021-08-26 14:00:59 +00:00
}
var payload namespacesToggleSystemPayload
err = request . DecodeAndValidateJSONPayload ( r , & payload )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . BadRequest ( "Invalid request payload" , err )
2021-08-26 14:00:59 +00:00
}
2024-10-01 01:15:51 +00:00
kubeClient , err := handler . KubernetesClientFactory . GetPrivilegedKubeClient ( endpoint )
2021-08-26 14:00:59 +00:00
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . InternalServerError ( "Unable to create kubernetes client" , err )
2021-08-26 14:00:59 +00:00
}
err = kubeClient . ToggleSystemState ( namespaceName , payload . System )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . InternalServerError ( "Unable to toggle system status" , err )
2021-08-26 14:00:59 +00:00
}
return response . Empty ( rw )
}