|
| 1 | +package volcengine |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/404tk/cloudtoolkit/pkg/providers/volcengine/billing" |
| 8 | + "github.com/404tk/cloudtoolkit/pkg/providers/volcengine/ecs" |
| 9 | + "github.com/404tk/cloudtoolkit/pkg/providers/volcengine/iam" |
| 10 | + "github.com/404tk/cloudtoolkit/pkg/schema" |
| 11 | + "github.com/404tk/cloudtoolkit/utils" |
| 12 | + "github.com/404tk/cloudtoolkit/utils/cache" |
| 13 | + "github.com/404tk/cloudtoolkit/utils/logger" |
| 14 | + "github.com/volcengine/volcengine-go-sdk/service/iam20210801" |
| 15 | + "github.com/volcengine/volcengine-go-sdk/volcengine" |
| 16 | + "github.com/volcengine/volcengine-go-sdk/volcengine/credentials" |
| 17 | + "github.com/volcengine/volcengine-go-sdk/volcengine/session" |
| 18 | +) |
| 19 | + |
| 20 | +type Provider struct { |
| 21 | + conf *volcengine.Config |
| 22 | + region string |
| 23 | +} |
| 24 | + |
| 25 | +// New creates a new provider client for alibaba API |
| 26 | +func New(options schema.Options) (*Provider, error) { |
| 27 | + accessKey, ok := options.GetMetadata(utils.AccessKey) |
| 28 | + if !ok { |
| 29 | + return nil, &schema.ErrNoSuchKey{Name: utils.AccessKey} |
| 30 | + } |
| 31 | + secretKey, ok := options.GetMetadata(utils.SecretKey) |
| 32 | + if !ok { |
| 33 | + return nil, &schema.ErrNoSuchKey{Name: utils.SecretKey} |
| 34 | + } |
| 35 | + region, _ := options.GetMetadata(utils.Region) |
| 36 | + token, _ := options.GetMetadata(utils.SecurityToken) |
| 37 | + cred := credentials.NewStaticCredentials(accessKey, secretKey, token) |
| 38 | + config := volcengine.NewConfig().WithCredentials(cred) |
| 39 | + |
| 40 | + payload, _ := options.GetMetadata(utils.Payload) |
| 41 | + if payload == "cloudlist" { |
| 42 | + name, err := getProject(region, config) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + cache.Cfg.CredInsert(name, options) |
| 47 | + } |
| 48 | + |
| 49 | + return &Provider{ |
| 50 | + conf: config, |
| 51 | + region: region, |
| 52 | + }, nil |
| 53 | +} |
| 54 | + |
| 55 | +func getProject(r string, conf *volcengine.Config) (string, error) { |
| 56 | + if r == "all" { |
| 57 | + conf = conf.WithRegion("cn-beijing") |
| 58 | + } else { |
| 59 | + conf = conf.WithRegion(r) |
| 60 | + } |
| 61 | + sess, _ := session.NewSession(conf) |
| 62 | + svc := iam20210801.New(sess) |
| 63 | + out, err := svc.ListProjects(&iam20210801.ListProjectsInput{}) |
| 64 | + if err != nil { |
| 65 | + return "", err |
| 66 | + } |
| 67 | + name := fmt.Sprintf("%s(%d)", *out.Projects[0].ProjectName, *out.Projects[0].AccountID) |
| 68 | + logger.Warning("Current project:", name) |
| 69 | + return name, nil |
| 70 | +} |
| 71 | + |
| 72 | +// Name returns the name of the provider |
| 73 | +func (p *Provider) Name() string { |
| 74 | + return "volcengine" |
| 75 | +} |
| 76 | + |
| 77 | +// Resources returns the provider for a resource deployment source. |
| 78 | +func (p *Provider) Resources(ctx context.Context) (schema.Resources, error) { |
| 79 | + list := schema.NewResources() |
| 80 | + list.Provider = p.Name() |
| 81 | + var err error |
| 82 | + for _, product := range utils.Cloudlist { |
| 83 | + switch product { |
| 84 | + case "balance": |
| 85 | + billing.QueryAccountBalance(p.conf) |
| 86 | + case "host": |
| 87 | + d := &ecs.Driver{Conf: p.conf, Region: p.region} |
| 88 | + list.Hosts, err = d.GetResource(ctx) |
| 89 | + case "domain": |
| 90 | + case "account": |
| 91 | + d := &iam.Driver{Conf: p.conf} |
| 92 | + list.Users, err = d.ListUsers(ctx) |
| 93 | + case "database": |
| 94 | + case "bucket": |
| 95 | + case "sms": |
| 96 | + case "log": |
| 97 | + default: |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return list, err |
| 102 | +} |
| 103 | + |
| 104 | +func (p *Provider) UserManagement(action, args_1, args_2 string) {} |
| 105 | + |
| 106 | +func (p *Provider) BucketDump(ctx context.Context, action, bucketname string) {} |
| 107 | + |
| 108 | +func (p *Provider) EventDump(action, args string) {} |
| 109 | + |
| 110 | +func (p *Provider) ExecuteCloudVMCommand(instanceId, cmd string) {} |
| 111 | + |
| 112 | +func (p *Provider) DBManagement(action, args string) {} |
0 commit comments