|
package server
|
Generated documentation is available at:
https://godoc.org/github.com/RedHatInsights/insights-operator-controller/server
Documentation in literate-programming-style is available at:
https://redhatinsights.github.io/insights-operator-controller/packages/server/profile.html
|
import (
"io"
"net/http"
"strconv"
"github.com/RedHatInsights/insights-operator-controller/storage"
"github.com/RedHatInsights/insights-operator-utils/responses"
)
|
ListConfigurationProfiles method reads list of configuration profiles.
|
func ( s Server ) ListConfigurationProfiles ( writer http . ResponseWriter , request * http . Request ) {
|
try to read list of configuration profiles from storage
|
profiles , err := s . Storage . ListConfigurationProfiles ( )
|
check if the storage operation was successful
|
if err == nil {
TryToSendOKServerResponse ( writer , responses . BuildOkResponseWithData ( "profiles" , profiles ) )
} else {
TryToSendInternalServerError ( writer , err . Error ( ) )
}
}
|
GetConfigurationProfile method reads profile specified by its ID
|
func ( s Server ) GetConfigurationProfile ( writer http . ResponseWriter , request * http . Request ) {
|
profile ID needs to be specified in request
|
id , err := retrieveIDRequestParameter ( request )
if err != nil {
TryToSendBadRequestServerResponse ( writer , "Error reading profile ID from request\n" )
return
}
|
try to read configuration for profile specified by its ID
|
profile , err := s . Storage . GetConfigurationProfile ( int ( id ) )
|
check if the storage operation was successful
|
if _ , ok := err . ( * storage . ItemNotFoundError ) ; ok {
TryToSendResponse ( http . StatusNotFound , writer , err . Error ( ) )
} else if err != nil {
TryToSendInternalServerError ( writer , err . Error ( ) )
} else {
TryToSendOKServerResponse ( writer , responses . BuildOkResponseWithData ( "profile" , profile ) )
}
}
|
NewConfigurationProfile method creates new configuration profile
|
func ( s Server ) NewConfigurationProfile ( writer http . ResponseWriter , request * http . Request ) {
|
username needs to be specified in request
|
username , foundUsername := request . URL . Query ( ) [ "username" ]
|
description needs to be specified in request
|
description , foundDescription := request . URL . Query ( ) [ "description" ]
if ! foundUsername {
TryToSendBadRequestServerResponse ( writer , "User name needs to be specified\n" )
return
}
if ! foundDescription {
TryToSendBadRequestServerResponse ( writer , "Description needs to be specified\n" )
return
}
|
read configuration from request body
|
configuration , err := io . ReadAll ( request . Body )
if err != nil || len ( configuration ) == 0 {
TryToSendBadRequestServerResponse ( writer , "Configuration needs to be provided in the request body" )
return
}
|
try to record the action NewConfigurationProfile into Splunk
|
err = s . Splunk . LogAction ( "NewConfigurationProfile" , username [ 0 ] , string ( configuration ) )
|
and check whether the Splunk operation was successful
|
checkSplunkOperation ( err )
|
try to store configuration profile into storage
|
profiles , err := s . Storage . StoreConfigurationProfile ( username [ 0 ] , description [ 0 ] , string ( configuration ) )
|
check if the storage operation was successful
|
if err != nil {
TryToSendInternalServerError ( writer , err . Error ( ) )
} else {
TryToSendCreatedServerResponse ( writer , responses . BuildOkResponseWithData ( "profiles" , profiles ) )
}
}
|
DeleteConfigurationProfile method deletes configuration profile
|
func ( s Server ) DeleteConfigurationProfile ( writer http . ResponseWriter , request * http . Request ) {
|
profile ID needs to be specified in request
|
id , err := retrieveIDRequestParameter ( request )
if err != nil {
TryToSendBadRequestServerResponse ( writer , "Error reading profile ID from request\n" )
return
}
|
try to record the action DeleteConfigurationProfile into Splunk
|
err = s . Splunk . LogAction ( "DeleteConfigurationProfile" , "tester" , strconv . Itoa ( int ( id ) ) )
|
and check whether the Splunk operation was successful
|
checkSplunkOperation ( err )
|
try to delete configuration profile from storage
|
profiles , err := s . Storage . DeleteConfigurationProfile ( int ( id ) )
|
check if the storage operation was successful
|
if _ , ok := err . ( * storage . ItemNotFoundError ) ; ok {
TryToSendResponse ( http . StatusNotFound , writer , err . Error ( ) )
} else if err != nil {
TryToSendInternalServerError ( writer , err . Error ( ) )
} else {
TryToSendOKServerResponse ( writer , responses . BuildOkResponseWithData ( "profiles" , profiles ) )
}
}
|
ChangeConfigurationProfile method changes configuration profile
|
func ( s Server ) ChangeConfigurationProfile ( writer http . ResponseWriter , request * http . Request ) {
|
profile ID needs to be specified in request
|
id , err := retrieveIDRequestParameter ( request )
|
username needs to be specified in request
|
username , foundUsername := request . URL . Query ( ) [ "username" ]
|
description needs to be specified in request
|
description , foundDescription := request . URL . Query ( ) [ "description" ]
if err != nil {
TryToSendBadRequestServerResponse ( writer , "Error reading profile ID from request\n" )
return
}
if ! foundUsername {
TryToSendBadRequestServerResponse ( writer , "User name needs to be specified\n" )
return
}
if ! foundDescription {
TryToSendBadRequestServerResponse ( writer , "Description needs to be specified\n" )
return
}
configuration , err := io . ReadAll ( request . Body )
if err != nil || len ( configuration ) == 0 {
TryToSendBadRequestServerResponse ( writer , "Configuration needs to be provided in the request body" )
return
}
|
try to record the action ChangeConfigurationProfile into Splunk
|
err = s . Splunk . LogAction ( "ChangeConfigurationProfile" , username [ 0 ] , string ( configuration ) )
|
and check whether the Splunk operation was successful
|
checkSplunkOperation ( err )
|
try to change configuration profile configuration in storage
|
profiles , err := s . Storage . ChangeConfigurationProfile ( int ( id ) , username [ 0 ] , description [ 0 ] , string ( configuration ) )
|
check if the storage operation was successful
|
if _ , ok := err . ( * storage . ItemNotFoundError ) ; ok {
TryToSendResponse ( http . StatusNotFound , writer , err . Error ( ) )
} else if err != nil {
TryToSendInternalServerError ( writer , err . Error ( ) )
} else {
TryToSendOKServerResponse ( writer , responses . BuildOkResponseWithData ( "profiles" , profiles ) )
}
}
|