2022-11-21 19:45:08 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-12-14 20:40:45 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/zrokEdgeSdk"
|
2022-11-21 19:45:08 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/model"
|
2023-01-04 19:43:37 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/share"
|
2022-11-21 19:45:08 +01:00
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
|
|
|
)
|
|
|
|
|
2022-11-22 21:31:02 +01:00
|
|
|
type publicResourceAllocator struct{}
|
2022-11-21 19:45:08 +01:00
|
|
|
|
2022-11-22 17:35:25 +01:00
|
|
|
func newPublicResourceAllocator() *publicResourceAllocator {
|
|
|
|
return &publicResourceAllocator{}
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
func (a *publicResourceAllocator) allocate(envZId, svcToken string, frontendZIds, frontendTemplates []string, params share.ShareParams, edge *rest_management_api_client.ZitiEdgeManagement) (svcZId string, frontendEndpoints []string, err error) {
|
2022-11-22 21:31:02 +01:00
|
|
|
var authUsers []*model.AuthUser
|
|
|
|
for _, authUser := range params.Body.AuthUsers {
|
|
|
|
authUsers = append(authUsers, &model.AuthUser{authUser.Username, authUser.Password})
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
2022-12-14 20:40:45 +01:00
|
|
|
cfgId, err := zrokEdgeSdk.CreateConfig(zrokProxyConfigId, envZId, svcToken, params.Body.AuthScheme, authUsers, edge)
|
2022-11-21 19:45:08 +01:00
|
|
|
if err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 20:40:45 +01:00
|
|
|
svcZId, err = zrokEdgeSdk.CreateShareService(envZId, svcToken, cfgId, edge)
|
2022-11-21 19:45:08 +01:00
|
|
|
if err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyBind(envZId+"-"+svcZId+"-bind", svcZId, envZId, zrokEdgeSdk.ZrokShareTags(svcToken).SubTags, edge); err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyDial(envZId+"-"+svcZId+"-dial", svcZId, frontendZIds, zrokEdgeSdk.ZrokShareTags(svcToken).SubTags, edge); err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2022-12-15 17:50:30 +01:00
|
|
|
if err := zrokEdgeSdk.CreateShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil {
|
2022-11-22 21:31:02 +01:00
|
|
|
return "", nil, err
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|
|
|
|
|
2022-12-06 17:48:59 +01:00
|
|
|
for _, frontendTemplate := range frontendTemplates {
|
|
|
|
frontendEndpoints = append(frontendEndpoints, proxyUrl(svcToken, frontendTemplate))
|
|
|
|
}
|
|
|
|
|
|
|
|
return svcZId, frontendEndpoints, nil
|
2022-11-21 19:45:08 +01:00
|
|
|
}
|