/
secret.go
51 lines (46 loc) · 1.54 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package secret
import (
"docker.io/go-docker/api/types"
"docker.io/go-docker/api/types/swarm"
"github.com/appcelerator/amp/pkg/docker"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Server server information
type Server struct {
Docker *docker.Docker
}
func (s *Server) SecretCreate(ctx context.Context, in *CreateRequest) (*CreateReply, error) {
spec := swarm.SecretSpec{
Annotations: swarm.Annotations{Name: in.Name},
Data: in.Data,
}
reply, err := s.Docker.GetClient().SecretCreate(ctx, spec)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
log.Infoln("Successfully created secret:", in.Name)
return &CreateReply{Id: reply.ID}, nil
}
func (s *Server) SecretList(ctx context.Context, in *ListRequest) (*ListReply, error) {
options := types.SecretListOptions{}
secrets, err := s.Docker.GetClient().SecretList(ctx, options)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
log.Infoln("Successfully listed secret")
reply := &ListReply{}
for _, secret := range secrets {
reply.Entries = append(reply.Entries, &SecretEntry{Id: secret.ID, Name: secret.Spec.Name})
}
return reply, nil
}
func (s *Server) SecretRemove(ctx context.Context, in *RemoveRequest) (*RemoveReply, error) {
if err := s.Docker.GetClient().SecretRemove(ctx, in.Id); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
log.Infoln("Successfully removed secret:", in.Id)
return &RemoveReply{Id: in.Id}, nil
}