2021-11-29 13:06:50 +00:00
|
|
|
package openamt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Profile struct {
|
|
|
|
ProfileName string `json:"profileName"`
|
|
|
|
Activation string `json:"activation"`
|
|
|
|
CIRAConfigName *string `json:"ciraConfigName"`
|
|
|
|
GenerateRandomAMTPassword bool `json:"generateRandomPassword"`
|
|
|
|
AMTPassword string `json:"amtPassword"`
|
|
|
|
GenerateRandomMEBxPassword bool `json:"generateRandomMEBxPassword"`
|
|
|
|
MEBXPassword string `json:"mebxPassword"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
DHCPEnabled bool `json:"dhcpEnabled"`
|
|
|
|
TenantId string `json:"tenantId"`
|
|
|
|
WIFIConfigs []ProfileWifiConfig `json:"wifiConfigs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
ProfileWifiConfig struct {
|
|
|
|
Priority int `json:"priority"`
|
|
|
|
ProfileName string `json:"profileName"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (service *Service) createOrUpdateAMTProfile(configuration portainer.OpenAMTConfiguration, profileName string, ciraConfigName string, wirelessConfig string) (*Profile, error) {
|
|
|
|
profile, err := service.getAMTProfile(configuration, profileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := http.MethodPost
|
|
|
|
if profile != nil {
|
|
|
|
method = http.MethodPatch
|
|
|
|
}
|
|
|
|
|
|
|
|
profile, err = service.saveAMTProfile(method, configuration, profileName, ciraConfigName, wirelessConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return profile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) getAMTProfile(configuration portainer.OpenAMTConfiguration, profileName string) (*Profile, error) {
|
2021-11-30 23:35:47 +00:00
|
|
|
url := fmt.Sprintf("https://%s/rps/api/v1/admin/profiles/%s", configuration.MPSServer, profileName)
|
2021-11-29 13:06:50 +00:00
|
|
|
|
|
|
|
responseBody, err := service.executeGetRequest(url, configuration.Credentials.MPSToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if responseBody == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result Profile
|
|
|
|
err = json.Unmarshal(responseBody, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) saveAMTProfile(method string, configuration portainer.OpenAMTConfiguration, profileName string, ciraConfigName string, wirelessConfig string) (*Profile, error) {
|
2021-11-30 23:35:47 +00:00
|
|
|
url := fmt.Sprintf("https://%s/rps/api/v1/admin/profiles", configuration.MPSServer)
|
2021-11-29 13:06:50 +00:00
|
|
|
|
|
|
|
profile := Profile{
|
|
|
|
ProfileName: profileName,
|
|
|
|
Activation: "acmactivate",
|
|
|
|
GenerateRandomAMTPassword: false,
|
|
|
|
GenerateRandomMEBxPassword: false,
|
|
|
|
AMTPassword: configuration.Credentials.MPSPassword,
|
|
|
|
MEBXPassword: configuration.Credentials.MPSPassword,
|
|
|
|
CIRAConfigName: &ciraConfigName,
|
|
|
|
Tags: []string{},
|
|
|
|
DHCPEnabled: true,
|
|
|
|
}
|
|
|
|
if wirelessConfig != "" {
|
|
|
|
profile.WIFIConfigs = []ProfileWifiConfig{
|
|
|
|
{
|
|
|
|
Priority: 1,
|
|
|
|
ProfileName: DefaultWirelessConfigName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
payload, _ := json.Marshal(profile)
|
|
|
|
|
|
|
|
responseBody, err := service.executeSaveRequest(method, url, configuration.Credentials.MPSToken, payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result Profile
|
|
|
|
err = json.Unmarshal(responseBody, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|