This commit is contained in:
Djordje Lukic 2020-05-04 09:57:52 +02:00
Родитель 885fd799e1
Коммит 152d4119d2
1 изменённых файлов: 18 добавлений и 18 удалений

Просмотреть файл

@ -14,8 +14,8 @@ import (
)
type containerService struct {
cgc containerinstance.ContainerGroupsClient
ctx store.AciContext
containerGroupsClient containerinstance.ContainerGroupsClient
ctx store.AciContext
}
func init() {
@ -29,56 +29,56 @@ func getter() interface{} {
}
func New(ctx context.Context) (containers.ContainerService, error) {
cc := apicontext.CurrentContext(ctx)
currentContext := apicontext.CurrentContext(ctx)
contextStore, err := store.New()
if err != nil {
return nil, err
}
metadata, err := contextStore.Get(cc, getter)
metadata, err := contextStore.Get(currentContext, getter)
if err != nil {
return nil, errors.Wrap(err, "wrong context type")
}
tc, _ := metadata.Metadata.Data.(store.AciContext)
aciContext, _ := metadata.Metadata.Data.(store.AciContext)
auth, _ := auth.NewAuthorizerFromCLI()
containerGroupsClient := containerinstance.NewContainerGroupsClient(tc.SubscriptionID)
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
containerGroupsClient.Authorizer = auth
return &containerService{
cgc: containerGroupsClient,
ctx: tc,
containerGroupsClient: containerGroupsClient,
ctx: aciContext,
}, nil
}
func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
var cg []containerinstance.ContainerGroup
result, err := cs.cgc.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
var containerGroups []containerinstance.ContainerGroup
result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
if err != nil {
return []containers.Container{}, err
}
for result.NotDone() {
cg = append(cg, result.Values()...)
containerGroups = append(containerGroups, result.Values()...)
if err := result.NextWithContext(ctx); err != nil {
return []containers.Container{}, err
}
}
res := []containers.Container{}
for _, c := range cg {
group, err := cs.cgc.Get(ctx, cs.ctx.ResourceGroup, *c.Name)
for _, containerGroup := range containerGroups {
group, err := cs.containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
if err != nil {
return []containers.Container{}, err
}
for _, d := range *group.Containers {
for _, container := range *group.Containers {
status := "Unknown"
if d.InstanceView != nil && d.InstanceView.CurrentState != nil {
status = *d.InstanceView.CurrentState.State
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
status = *container.InstanceView.CurrentState.State
}
res = append(res, containers.Container{
ID: *d.Name,
Image: *d.Image,
ID: *container.Name,
Image: *container.Image,
Status: status,
})
}