|
| 1 | +package instance |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/go-kit/kit/log" |
| 7 | + "github.com/go-kit/kit/log/level" |
| 8 | + sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328" |
| 9 | + |
| 10 | + "github.com/tencentyun/tencentcloud-exporter/pkg/client" |
| 11 | + "github.com/tencentyun/tencentcloud-exporter/pkg/config" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + registerRepository("QCE/SQLSERVER", NewSqlServerTcInstanceRepository) |
| 16 | +} |
| 17 | + |
| 18 | +type SqlServerTcInstanceRepository struct { |
| 19 | + client *sdk.Client |
| 20 | + logger log.Logger |
| 21 | +} |
| 22 | + |
| 23 | +func (repo *SqlServerTcInstanceRepository) GetInstanceKey() string { |
| 24 | + return "InstanceId" |
| 25 | +} |
| 26 | + |
| 27 | +func (repo *SqlServerTcInstanceRepository) Get(id string) (instance TcInstance, err error) { |
| 28 | + req := sdk.NewDescribeDBInstancesRequest() |
| 29 | + req.InstanceIdSet = []*string{&id} |
| 30 | + resp, err := repo.client.DescribeDBInstances(req) |
| 31 | + if err != nil { |
| 32 | + return |
| 33 | + } |
| 34 | + if len(resp.Response.DBInstances) != 1 { |
| 35 | + return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) |
| 36 | + } |
| 37 | + meta := resp.Response.DBInstances[0] |
| 38 | + instance, err = NewSqlServerTcInstance(id, meta) |
| 39 | + if err != nil { |
| 40 | + return |
| 41 | + } |
| 42 | + return |
| 43 | +} |
| 44 | + |
| 45 | +func (repo *SqlServerTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { |
| 46 | + return |
| 47 | +} |
| 48 | + |
| 49 | +func (repo *SqlServerTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { |
| 50 | + req := sdk.NewDescribeDBInstancesRequest() |
| 51 | + var offset int64 = 0 |
| 52 | + var limit int64 = 100 |
| 53 | + var total int64 = -1 |
| 54 | + |
| 55 | + req.Offset = &offset |
| 56 | + req.Limit = &limit |
| 57 | + |
| 58 | +getMoreInstances: |
| 59 | + resp, err := repo.client.DescribeDBInstances(req) |
| 60 | + if err != nil { |
| 61 | + return |
| 62 | + } |
| 63 | + if total == -1 { |
| 64 | + total = *resp.Response.TotalCount |
| 65 | + } |
| 66 | + for _, meta := range resp.Response.DBInstances { |
| 67 | + ins, e := NewSqlServerTcInstance(*meta.InstanceId, meta) |
| 68 | + if e != nil { |
| 69 | + level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId) |
| 70 | + continue |
| 71 | + } |
| 72 | + instances = append(instances, ins) |
| 73 | + } |
| 74 | + offset += limit |
| 75 | + if offset < total { |
| 76 | + req.Offset = &offset |
| 77 | + goto getMoreInstances |
| 78 | + } |
| 79 | + |
| 80 | + return |
| 81 | +} |
| 82 | + |
| 83 | +func NewSqlServerTcInstanceRepository(c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { |
| 84 | + cli, err := client.NewSqlServerClient(c) |
| 85 | + if err != nil { |
| 86 | + return |
| 87 | + } |
| 88 | + repo = &SqlServerTcInstanceRepository{ |
| 89 | + client: cli, |
| 90 | + logger: logger, |
| 91 | + } |
| 92 | + return |
| 93 | +} |
0 commit comments