/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ctl
import (
"fmt"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipui/internal/configs"
)
// HandleConfigRequest will flop between requests so we don't have to have them all mapped as function calls
func HandleConfigRequest(request configs.WsMessage) configs.WsMessage {
response := configs.WsMessage{
Type: configs.AirshipCTL,
Component: configs.CTLConfig,
SubComponent: request.SubComponent,
}
var err error
var message string
switch request.SubComponent {
case configs.GetDefaults:
response.HTML, err = getConfigHTML()
case configs.SetContext:
message, err = setContext(request)
case configs.SetCluster:
message, err = setCluster(request)
case configs.SetCredential:
message, err = setCredential(request)
default:
err = fmt.Errorf("Subcomponent %s not found", request.SubComponent)
}
if err != nil {
response.Error = err.Error()
} else {
response.Message = message
}
return response
}
// GetCluster gets cluster information from the airshipctl config
func (c *Client) getCluster() []*config.Cluster {
return c.settings.Config.GetClusters()
}
// getClusterTableRows turns an array of cluster into html table rows
func getClusterTableRows() string {
info := c.getCluster()
var rows string
for _, config := range info {
// TODO: all rows are editable, probably shouldn't be
rows += "
" +
config.Bootstrap + "
" +
config.NameInKubeconf + "
" +
config.ManagementConfiguration + "
" +
config.KubeCluster().LocationOfOrigin + "
" +
config.KubeCluster().Server + "
" +
config.KubeCluster().CertificateAuthority + "
" +
"
"
}
return rows
}
// GetContext gets cluster information from the airshipctl config
func (c *Client) getContext() []*config.Context {
return c.settings.Config.GetContexts()
}
// getContextTableRows turns an array of contexts into html table rows
func getContextTableRows() string {
info := c.getContext()
var rows string
for _, context := range info {
// TODO: all rows are editable, probably shouldn't be
rows += "
" +
context.NameInKubeconf + "
" +
context.Manifest + "
" +
context.KubeContext().LocationOfOrigin + "
" +
context.KubeContext().Cluster + "
" +
context.KubeContext().AuthInfo + "
" +
"
"
}
return rows
}
// GetCredential gets user credentials from the airshipctl config
func (c *Client) getCredential() []*config.AuthInfo {
authinfo, err := c.settings.Config.GetAuthInfos()
if err != nil {
return []*config.AuthInfo{}
}
return authinfo
}
// getContextTableRows turns an array of contexts into html table rows
func getCredentialTableRows() string {
info := c.getCredential()
var rows string
for _, credential := range info {
// TODO: all rows are editable, probably shouldn't be
rows += "