|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Yunshan Networks |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aws |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/aws/aws-sdk-go-v2/config" |
| 23 | + "github.com/aws/aws-sdk-go-v2/service/rds" |
| 24 | + "github.com/aws/aws-sdk-go-v2/service/rds/types" |
| 25 | + "github.com/deepflowio/deepflow/server/controller/cloud/model" |
| 26 | + "github.com/deepflowio/deepflow/server/controller/common" |
| 27 | + "github.com/deepflowio/deepflow/server/libs/logger" |
| 28 | +) |
| 29 | + |
| 30 | +var rdsTypeEnums = map[string]int{ |
| 31 | + "mysql": common.RDS_TYPE_MYSQL, |
| 32 | + "sqlserver-ex": common.RDS_TYPE_SQL_SERVER, |
| 33 | + "sqlserver-web": common.RDS_TYPE_SQL_SERVER, |
| 34 | + "sqlserver-se": common.RDS_TYPE_SQL_SERVER, |
| 35 | + "sqlserver-ee": common.RDS_TYPE_SQL_SERVER, |
| 36 | + "postgres": common.RDS_TYPE_PSQL, |
| 37 | + "mariadb": common.RDS_TYPE_MARIADB, |
| 38 | + "oracle-ee": common.RDS_TYPE_ORACLE, |
| 39 | + "oracle-se2": common.RDS_TYPE_ORACLE, |
| 40 | +} |
| 41 | + |
| 42 | +func (a *Aws) getRDSInstances(region string) ([]model.RDSInstance, error) { |
| 43 | + log.Debug("get rds instances starting", logger.NewORGPrefix(a.orgID)) |
| 44 | + var rdss []model.RDSInstance |
| 45 | + |
| 46 | + rdsClientConfig, err := config.LoadDefaultConfig(context.TODO(), a.credential, config.WithRegion(region), config.WithHTTPClient(a.httpClient)) |
| 47 | + if err != nil { |
| 48 | + log.Error("client config failed (%s)", err.Error(), logger.NewORGPrefix(a.orgID)) |
| 49 | + return []model.RDSInstance{}, err |
| 50 | + } |
| 51 | + |
| 52 | + var retRDS []types.DBInstance |
| 53 | + var marker string |
| 54 | + var maxRecords int32 = 100 |
| 55 | + for { |
| 56 | + var input *rds.DescribeDBInstancesInput |
| 57 | + if marker == "" { |
| 58 | + input = &rds.DescribeDBInstancesInput{MaxRecords: &maxRecords} |
| 59 | + } else { |
| 60 | + input = &rds.DescribeDBInstancesInput{MaxRecords: &maxRecords, Marker: &marker} |
| 61 | + } |
| 62 | + result, err := rds.NewFromConfig(rdsClientConfig).DescribeDBInstances(context.TODO(), input) |
| 63 | + if err != nil { |
| 64 | + log.Errorf("rds request aws api error: (%s)", err.Error(), logger.NewORGPrefix(a.orgID)) |
| 65 | + return []model.RDSInstance{}, err |
| 66 | + } |
| 67 | + retRDS = append(retRDS, result.DBInstances...) |
| 68 | + if result.Marker == nil { |
| 69 | + break |
| 70 | + } |
| 71 | + marker = *result.Marker |
| 72 | + } |
| 73 | + for _, rds := range retRDS { |
| 74 | + rdsID := a.getStringPointerValue(rds.DbiResourceId) |
| 75 | + rdsLcuuid := common.GetUUIDByOrgID(a.orgID, rdsID) |
| 76 | + rdsName := a.getStringPointerValue(rds.DBInstanceIdentifier) |
| 77 | + rdsEngine := a.getStringPointerValue(rds.Engine) |
| 78 | + rdsType, ok := rdsTypeEnums[rdsEngine] |
| 79 | + if !ok { |
| 80 | + log.Infof("rds (%s) engine (%s) is not supported", rdsName, rdsEngine, logger.NewORGPrefix(a.orgID)) |
| 81 | + continue |
| 82 | + } |
| 83 | + azLcuuid := common.GetUUIDByOrgID(a.orgID, a.getStringPointerValue(rds.AvailabilityZone)) |
| 84 | + vpcLcuuid := common.GetUUIDByOrgID(a.orgID, a.getStringPointerValue(rds.DBSubnetGroup.VpcId)) |
| 85 | + var rdsState = common.RDS_STATE_RUNNING |
| 86 | + if a.getStringPointerValue(rds.DBInstanceStatus) != "available" { |
| 87 | + rdsState = common.RDS_STATE_RESTORING |
| 88 | + } |
| 89 | + rdss = append(rdss, model.RDSInstance{ |
| 90 | + Lcuuid: rdsLcuuid, |
| 91 | + Name: rdsName, |
| 92 | + Label: rdsID, |
| 93 | + State: rdsState, |
| 94 | + Type: rdsType, |
| 95 | + Series: common.RDS_SERIES_BASIC, |
| 96 | + Version: a.getStringPointerValue(rds.EngineVersion), |
| 97 | + Model: common.RDS_MODEL_PRIMARY, |
| 98 | + VPCLcuuid: vpcLcuuid, |
| 99 | + AZLcuuid: azLcuuid, |
| 100 | + RegionLcuuid: a.regionLcuuid, |
| 101 | + }) |
| 102 | + a.azLcuuidMap[azLcuuid] = 0 |
| 103 | + } |
| 104 | + log.Debug("get rds instances complete", logger.NewORGPrefix(a.orgID)) |
| 105 | + return rdss, nil |
| 106 | +} |
0 commit comments