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"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
func (service *Service) createOrUpdateAMTProfile(configuration portainer.OpenAMTConfiguration, profileName string, ciraConfigName string) (*Profile, error) {
|
2021-11-29 13:06:50 +00:00
|
|
|
profile, err := service.getAMTProfile(configuration, profileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := http.MethodPost
|
|
|
|
if profile != nil {
|
|
|
|
method = http.MethodPatch
|
|
|
|
}
|
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
profile, err = service.saveAMTProfile(method, configuration, profileName, ciraConfigName)
|
2021-11-29 13:06:50 +00:00
|
|
|
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
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
responseBody, err := service.executeGetRequest(url, configuration.MPSToken)
|
2021-11-29 13:06:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
func (service *Service) saveAMTProfile(method string, configuration portainer.OpenAMTConfiguration, profileName string, ciraConfigName 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,
|
2022-01-23 19:48:04 +00:00
|
|
|
AMTPassword: configuration.MPSPassword,
|
|
|
|
MEBXPassword: configuration.MPSPassword,
|
2021-11-29 13:06:50 +00:00
|
|
|
CIRAConfigName: &ciraConfigName,
|
|
|
|
Tags: []string{},
|
|
|
|
DHCPEnabled: true,
|
|
|
|
}
|
|
|
|
payload, _ := json.Marshal(profile)
|
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
responseBody, err := service.executeSaveRequest(method, url, configuration.MPSToken, payload)
|
2021-11-29 13:06:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result Profile
|
|
|
|
err = json.Unmarshal(responseBody, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|