2021-11-29 13:06:50 +00:00
|
|
|
package openamt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Domain struct {
|
|
|
|
DomainName string `json:"profileName"`
|
|
|
|
DomainSuffix string `json:"domainSuffix"`
|
|
|
|
ProvisioningCert string `json:"provisioningCert"`
|
|
|
|
ProvisioningCertPassword string `json:"provisioningCertPassword"`
|
|
|
|
ProvisioningCertStorageFormat string `json:"provisioningCertStorageFormat"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (service *Service) createOrUpdateDomain(configuration portainer.OpenAMTConfiguration) (*Domain, error) {
|
|
|
|
domain, err := service.getDomain(configuration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := http.MethodPost
|
|
|
|
if domain != nil {
|
|
|
|
method = http.MethodPatch
|
|
|
|
}
|
|
|
|
|
|
|
|
domain, err = service.saveDomain(method, configuration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) getDomain(configuration portainer.OpenAMTConfiguration) (*Domain, error) {
|
2022-01-23 19:48:04 +00:00
|
|
|
url := fmt.Sprintf("https://%s/rps/api/v1/admin/domains/%s", configuration.MPSServer, configuration.DomainName)
|
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 Domain
|
|
|
|
err = json.Unmarshal(responseBody, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) saveDomain(method string, configuration portainer.OpenAMTConfiguration) (*Domain, error) {
|
2021-11-30 23:35:47 +00:00
|
|
|
url := fmt.Sprintf("https://%s/rps/api/v1/admin/domains", configuration.MPSServer)
|
2021-11-29 13:06:50 +00:00
|
|
|
|
|
|
|
profile := Domain{
|
2022-01-23 19:48:04 +00:00
|
|
|
DomainName: configuration.DomainName,
|
|
|
|
DomainSuffix: configuration.DomainName,
|
|
|
|
ProvisioningCert: configuration.CertFileContent,
|
|
|
|
ProvisioningCertPassword: configuration.CertFilePassword,
|
2021-11-29 13:06:50 +00:00
|
|
|
ProvisioningCertStorageFormat: "string",
|
|
|
|
}
|
|
|
|
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 Domain
|
|
|
|
err = json.Unmarshal(responseBody, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|