2020-04-29 20:57:53 +03:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
|
|
|
|
"github.com/Azure/go-autorest/autorest/azure/auth"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/docker/api/backend"
|
|
|
|
"github.com/docker/api/containers"
|
|
|
|
apicontext "github.com/docker/api/context"
|
|
|
|
"github.com/docker/api/context/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type containerService struct {
|
2020-05-04 10:57:52 +03:00
|
|
|
containerGroupsClient containerinstance.ContainerGroupsClient
|
|
|
|
ctx store.AciContext
|
2020-04-29 20:57:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
|
|
|
|
return New(ctx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getter() interface{} {
|
|
|
|
return &store.AciContext{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx context.Context) (containers.ContainerService, error) {
|
2020-05-04 10:57:52 +03:00
|
|
|
currentContext := apicontext.CurrentContext(ctx)
|
2020-04-30 00:44:01 +03:00
|
|
|
contextStore, err := store.New()
|
2020-04-29 20:57:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-04 10:57:52 +03:00
|
|
|
metadata, err := contextStore.Get(currentContext, getter)
|
2020-04-29 20:57:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "wrong context type")
|
|
|
|
}
|
2020-05-04 10:57:52 +03:00
|
|
|
aciContext, _ := metadata.Metadata.Data.(store.AciContext)
|
2020-04-29 20:57:53 +03:00
|
|
|
|
|
|
|
auth, _ := auth.NewAuthorizerFromCLI()
|
2020-05-04 10:57:52 +03:00
|
|
|
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
|
2020-04-29 20:57:53 +03:00
|
|
|
containerGroupsClient.Authorizer = auth
|
|
|
|
|
|
|
|
return &containerService{
|
2020-05-04 10:57:52 +03:00
|
|
|
containerGroupsClient: containerGroupsClient,
|
|
|
|
ctx: aciContext,
|
2020-04-29 20:57:53 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
|
2020-05-04 10:57:52 +03:00
|
|
|
var containerGroups []containerinstance.ContainerGroup
|
|
|
|
result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
|
2020-04-29 20:57:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return []containers.Container{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.NotDone() {
|
2020-05-04 10:57:52 +03:00
|
|
|
containerGroups = append(containerGroups, result.Values()...)
|
2020-04-29 20:57:53 +03:00
|
|
|
if err := result.NextWithContext(ctx); err != nil {
|
|
|
|
return []containers.Container{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := []containers.Container{}
|
2020-05-04 10:57:52 +03:00
|
|
|
for _, containerGroup := range containerGroups {
|
|
|
|
group, err := cs.containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
|
2020-05-03 15:51:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return []containers.Container{}, err
|
|
|
|
}
|
|
|
|
|
2020-05-04 10:57:52 +03:00
|
|
|
for _, container := range *group.Containers {
|
2020-05-03 15:51:03 +03:00
|
|
|
status := "Unknown"
|
2020-05-04 10:57:52 +03:00
|
|
|
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
|
|
|
|
status = *container.InstanceView.CurrentState.State
|
2020-05-03 15:51:03 +03:00
|
|
|
}
|
2020-04-29 20:57:53 +03:00
|
|
|
res = append(res, containers.Container{
|
2020-05-04 10:57:52 +03:00
|
|
|
ID: *container.Name,
|
|
|
|
Image: *container.Image,
|
2020-05-03 15:51:03 +03:00
|
|
|
Status: status,
|
2020-04-29 20:57:53 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|