From 5e77a9682e4c5aafc078563c31b240cf82926cc7 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Thu, 28 May 2020 15:23:25 +0800 Subject: [PATCH] fix: generating bucket APIs --- pkg/apis/compute/bucket.go | 62 ++++++++++++ pkg/apis/compute/zz_generated.model.go | 76 +++++++++----- pkg/cloudprovider/objectstore.go | 27 ++--- pkg/compute/models/buckets.go | 131 +++++++++++++++---------- pkg/compute/models/guestcdrom.go | 1 + pkg/compute/models/netinterfaces.go | 1 + pkg/multicloud/azure/shell/network.go | 2 +- 7 files changed, 211 insertions(+), 89 deletions(-) diff --git a/pkg/apis/compute/bucket.go b/pkg/apis/compute/bucket.go index a1d0502385..9eab87c435 100644 --- a/pkg/apis/compute/bucket.go +++ b/pkg/apis/compute/bucket.go @@ -120,3 +120,65 @@ type BucketSyncstatusInput struct { type BucketUpdateInput struct { apis.SharableVirtualResourceBaseUpdateInput } + +type BucketPerformTempUrlInput struct { + // 访问对象方法 + Method string `json:"method"` + // 对象KEY + // required:true + Key string `json:"key"` + // 过期时间,单位秒 + ExpireSeconds *int `json:"expire_seconds"` +} + +type BucketPerformTempUrlOutput struct { + // 生成的临时URL + Url string `json:"url"` +} + +type BucketPerformMakedirInput struct { + // 目录对象KEY + // required:true + Key string `json:"key"` +} + +type BucketPerformDeleteInput struct { + // 待删除对象KEY + // required:true + Keys []string `json:"keys"` +} + +type BucketGetAclInput struct { + // 对象KEY + // required:false + Key string `json:"key"` +} + +type BucketGetAclOutput struct { + // ACL + Acl string `json:"acl"` +} + +type BucketGetObjectsInput struct { + // Prefix + Prefix string `json:"prefix"` + // 是否模拟列举目录模式 + Recursive *bool `json:"recursive"` + // 分页标识 + PagingMarker string `json:"paging_marker"` + // 最大输出条目数 + Limit *int `json:"limit"` +} + +type BucketGetObjectsOutput struct { + // 对象列表 + Data []cloudprovider.SCloudObject `json:"data"` + // 排序字段,总是key + // example: key + MarkerField string `json:"marker_field"` + // 排序顺序,总是降序 + // example: DESC + MarkerOrder string `json:"marker_order"` + // 下一页请求的paging_marker标识 + NextMarker string `json:"next_marker"` +} diff --git a/pkg/apis/compute/zz_generated.model.go b/pkg/apis/compute/zz_generated.model.go index 7605795707..9ff9224597 100644 --- a/pkg/apis/compute/zz_generated.model.go +++ b/pkg/apis/compute/zz_generated.model.go @@ -864,6 +864,20 @@ type SGuestTemplateResourceBase struct { GuestTemplateId string `json:"guest_template_id"` } +// SGuestcdrom is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SGuestcdrom. +type SGuestcdrom struct { + Id string `json:"id"` + // = Column(VARCHAR(36, charset='ascii'), primary_key=True) + ImageId string `json:"image_id"` + // Column(VARCHAR(36, charset='ascii'), nullable=True) + Name string `json:"name"` + // Column(VARCHAR(64, charset='ascii'), nullable=True) + Path string `json:"path"` + // Column(VARCHAR(256, charset='ascii'), nullable=True) + Size int `json:"size"` + UpdateVersion int `json:"update_version"` +} + // SGuestdisk is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SGuestdisk. type SGuestdisk struct { SGuestJointsBase @@ -981,11 +995,10 @@ type SHost struct { OvnVersion string `json:"ovn_version"` IsBaremetal bool `json:"is_baremetal"` // 是否处于维护状态 - IsMaintenance bool `json:"is_maintenance"` - LastPingAt time.Time `json:"last_ping_at"` - EnableHealthCheck bool `json:"enable_health_check"` - ResourceType string `json:"resource_type"` - RealExternalId string `json:"real_external_id"` + IsMaintenance bool `json:"is_maintenance"` + EnableHealthCheck bool `json:"enable_health_check"` + ResourceType string `json:"resource_type"` + RealExternalId string `json:"real_external_id"` // 是否为导入的宿主机 IsImport bool `json:"is_import"` // 是否允许PXE启动 @@ -1242,12 +1255,9 @@ type SLoadbalancerAgentDeployment struct { // SLoadbalancerAgentParams is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SLoadbalancerAgentParams. type SLoadbalancerAgentParams struct { - KeepalivedConfTmpl string `json:"keepalived_conf_tmpl"` - HaproxyConfTmpl string `json:"haproxy_conf_tmpl"` - TelegrafConfTmpl string `json:"telegraf_conf_tmpl"` - Vrrp SLoadbalancerAgentParamsVrrp `json:"vrrp"` - Haproxy SLoadbalancerAgentParamsHaproxy `json:"haproxy"` - Telegraf SLoadbalancerAgentParamsTelegraf `json:"telegraf"` + KeepalivedConfTmpl string `json:"keepalived_conf_tmpl"` + HaproxyConfTmpl string `json:"haproxy_conf_tmpl"` + TelegrafConfTmpl string `json:"telegraf_conf_tmpl"` } // SLoadbalancerAgentParamsHaproxy is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SLoadbalancerAgentParamsHaproxy. @@ -1489,6 +1499,16 @@ type SManagedResourceBase struct { ManagerId string `json:"manager_id"` } +// SNatDEntry is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SNatDEntry. +type SNatDEntry struct { + SNatEntry + ExternalIP string `json:"external_ip"` + ExternalPort int `json:"external_port"` + InternalIP string `json:"internal_ip"` + InternalPort int `json:"internal_port"` + IpProtocol string `json:"ip_protocol"` +} + // SNatEntry is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SNatEntry. type SNatEntry struct { apis.SStatusInfrasResourceBase @@ -1519,6 +1539,25 @@ type SNatgatewayResourceBase struct { NatgatewayId string `json:"natgateway_id"` } +// SNetInterface is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SNetInterface. +type SNetInterface struct { + Mac string `json:"mac"` + // Column(VARCHAR(36, charset='ascii'), primary_key=True) + BaremetalId string `json:"baremetal_id"` + // Column(VARCHAR(36, charset='ascii'), nullable=True) + WireId string `json:"wire_id"` + // Column(VARCHAR(36, charset='ascii'), nullable=True) + Rate int `json:"rate"` + // Column(Integer, nullable=True) # Mbps + NicType string `json:"nic_type"` + // Column(VARCHAR(36, charset='ascii'), nullable=True) + Index byte `json:"index"` + // Column(TINYINT, nullable=True) + LinkUp bool `json:"link_up"` + // Column(Boolean, nullable=True) + Mtu int16 `json:"mtu"` +} + // SNetwork is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SNetwork. type SNetwork struct { apis.SSharableVirtualResourceBase @@ -1598,7 +1637,6 @@ type SPolicyAssignment struct { type SPolicyDefinition struct { apis.SStatusStandaloneResourceBase apis.SExternalizedResourceBase - SManagedResourceBase // 参数 Parameters interface{} `json:"parameters"` // 条件 @@ -1694,8 +1732,6 @@ type SScalingAlarm struct { Value float64 `json:"value"` // Real-time cumulate number RealCumulate int `json:"real_cumulate"` - // Last trigger time - LastTriggerTime time.Time `json:"last_trigger_time"` } // SScalingGroup is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SScalingGroup. @@ -1726,10 +1762,6 @@ type SScalingGroup struct { HealthCheckGov int `json:"health_check_gov"` LoadbalancerBackendPort int `json:"loadbalancer_backend_port"` LoadbalancerBackendWeight int `json:"loadbalancer_backend_weight"` - // Time to allow scale - AllowScaleTime time.Time `json:"allow_scale_time"` - // NextCheckTime descripe the next time to check instance's health - NextCheckTime time.Time `json:"next_check_time"` } // SScalingGroupGuest is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SScalingGroupGuest. @@ -1788,13 +1820,7 @@ type SScalingTimer struct { WeekDays byte `json:"week_days"` // 0-31 0 is unlimited MonthDays uint32 `json:"month_days"` - // StartTime represent the start time of this timer - StartTime time.Time `json:"start_time"` - // EndTime represent deadline of this timer - EndTime time.Time `json:"end_time"` - // NextTime represent the time timer should bell - NextTime time.Time `json:"next_time"` - IsExpired bool `json:"is_expired"` + IsExpired bool `json:"is_expired"` } // SSchedpolicy is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SSchedpolicy. diff --git a/pkg/cloudprovider/objectstore.go b/pkg/cloudprovider/objectstore.go index 498aa6108e..0463455499 100644 --- a/pkg/cloudprovider/objectstore.go +++ b/pkg/cloudprovider/objectstore.go @@ -183,16 +183,18 @@ type ICloudObject interface { SetAcl(acl TBucketACLType) error } -func ICloudObject2JSONObject(obj ICloudObject) jsonutils.JSONObject { - obj2 := struct { - Key string - SizeBytes int64 - StorageClass string - ETag string - LastModified time.Time - Meta http.Header - Acl string - }{ +type SCloudObject struct { + Key string + SizeBytes int64 + StorageClass string + ETag string + LastModified time.Time + Meta http.Header + Acl string +} + +func ICloudObject2Struct(obj ICloudObject) SCloudObject { + return SCloudObject{ Key: obj.GetKey(), SizeBytes: obj.GetSizeBytes(), StorageClass: obj.GetStorageClass(), @@ -201,7 +203,10 @@ func ICloudObject2JSONObject(obj ICloudObject) jsonutils.JSONObject { Meta: obj.GetMeta(), Acl: string(obj.GetAcl()), } - return jsonutils.Marshal(obj2) +} + +func ICloudObject2JSONObject(obj ICloudObject) jsonutils.JSONObject { + return jsonutils.Marshal(ICloudObject2Struct(obj)) } func (o *SBaseCloudObject) GetKey() string { diff --git a/pkg/compute/models/buckets.go b/pkg/compute/models/buckets.go index 4993c3e941..483c75b886 100644 --- a/pkg/compute/models/buckets.go +++ b/pkg/compute/models/buckets.go @@ -674,31 +674,41 @@ func (manager *SBucketManager) OrderByExtraFields( func (bucket *SBucket) AllowGetDetailsObjects( ctx context.Context, userCred mcclient.TokenCredential, - query jsonutils.JSONObject, + input api.BucketGetObjectsInput, ) bool { return bucket.IsOwner(userCred) } +// 获取bucket的对象列表 +// +// 获取bucket的对象列表 func (bucket *SBucket) GetDetailsObjects( ctx context.Context, userCred mcclient.TokenCredential, - query jsonutils.JSONObject, -) (jsonutils.JSONObject, error) { + input api.BucketGetObjectsInput, +) (api.BucketGetObjectsOutput, error) { + output := api.BucketGetObjectsOutput{} if len(bucket.ExternalId) == 0 { - return nil, httperrors.NewInvalidStatusError("no external bucket") + return output, httperrors.NewInvalidStatusError("no external bucket") } iBucket, err := bucket.GetIBucket() if err != nil { if errors.Cause(err) == httperrors.ErrInvalidStatus { - return nil, httperrors.NewInvalidStatusError("%s", err) + return output, httperrors.NewInvalidStatusError("%s", err) } else { - return nil, httperrors.NewInternalServerError("fail to find external bucket: %s", err) + return output, httperrors.NewInternalServerError("fail to find external bucket: %s", err) } } - prefix, _ := query.GetString("prefix") - isRecursive := jsonutils.QueryBoolean(query, "recursive", false) - marker, _ := query.GetString("paging_marker") - limit, _ := query.Int("limit") + prefix := input.Prefix + isRecursive := false + if input.Recursive != nil { + isRecursive = *input.Recursive + } + marker := input.PagingMarker + limit := 0 + if input.Limit != nil { + limit = *input.Limit + } if limit <= 0 { limit = 50 } else if limit > 1000 { @@ -706,49 +716,54 @@ func (bucket *SBucket) GetDetailsObjects( } objects, nextMarker, err := cloudprovider.GetPagedObjects(iBucket, prefix, isRecursive, marker, int(limit)) if err != nil { - return nil, httperrors.NewInternalServerError("fail to get objects: %s", err) + return output, httperrors.NewInternalServerError("fail to get objects: %s", err) } - retArray := jsonutils.NewArray() for i := range objects { - retArray.Add(cloudprovider.ICloudObject2JSONObject(objects[i])) + output.Data = append(output.Data, cloudprovider.ICloudObject2Struct(objects[i])) } - ret := jsonutils.NewDict() - ret.Add(retArray, "data") - ret.Add(jsonutils.NewString("key"), "marker_field") - ret.Add(jsonutils.NewString("DESC"), "marker_order") + output.MarkerField = "key" + output.MarkerOrder = "DESC" if len(nextMarker) > 0 { - ret.Add(jsonutils.NewString(nextMarker), "next_marker") + output.NextMarker = nextMarker } - return ret, nil + return output, nil } func (bucket *SBucket) AllowPerformTempUrl(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, - data jsonutils.JSONObject, + input api.BucketPerformTempUrlInput, ) bool { return bucket.IsOwner(userCred) } +// 获取访问对象的临时URL +// +// 获取访问对象的临时URL func (bucket *SBucket) PerformTempUrl( ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, - data jsonutils.JSONObject, -) (jsonutils.JSONObject, error) { + input api.BucketPerformTempUrlInput, +) (api.BucketPerformTempUrlOutput, error) { + output := api.BucketPerformTempUrlOutput{} + if len(bucket.ExternalId) == 0 { - return nil, httperrors.NewInvalidStatusError("no external bucket") + return output, httperrors.NewInvalidStatusError("no external bucket") } - method, _ := data.GetString("method") - key, _ := data.GetString("key") - expire, _ := data.Int("expire_seconds") + method := input.Method + key := input.Key + expire := 0 + if input.ExpireSeconds != nil { + expire = *input.ExpireSeconds + } if len(method) == 0 { method = "GET" } if len(key) == 0 { - return nil, httperrors.NewInputParameterError("missing key") + return output, httperrors.NewInputParameterError("missing key") } if expire == 0 { expire = 60 // default 60 seconds @@ -757,18 +772,17 @@ func (bucket *SBucket) PerformTempUrl( iBucket, err := bucket.GetIBucket() if err != nil { if errors.Cause(err) == httperrors.ErrInvalidStatus { - return nil, httperrors.NewInvalidStatusError("%s", err) + return output, httperrors.NewInvalidStatusError("%s", err) } else { - return nil, httperrors.NewInternalServerError("fail to find external bucket: %s", err) + return output, httperrors.NewInternalServerError("fail to find external bucket: %s", err) } } tmpUrl, err := iBucket.GetTempUrl(method, key, time.Duration(expire)*time.Second) if err != nil { - return nil, httperrors.NewInternalServerError("fail to generate temp url: %s", err) + return output, httperrors.NewInternalServerError("fail to generate temp url: %s", err) } - ret := jsonutils.NewDict() - ret.Add(jsonutils.NewString(tmpUrl), "url") - return ret, nil + output.Url = tmpUrl + return output, nil } func (bucket *SBucket) AllowPerformMakedir(ctx context.Context, @@ -779,17 +793,20 @@ func (bucket *SBucket) AllowPerformMakedir(ctx context.Context, return bucket.IsOwner(userCred) } +// 新建对象目录 +// +// 新建对象目录 func (bucket *SBucket) PerformMakedir( ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, - data jsonutils.JSONObject, + input api.BucketPerformMakedirInput, ) (jsonutils.JSONObject, error) { if len(bucket.ExternalId) == 0 { return nil, httperrors.NewInvalidStatusError("no external bucket") } - key, _ := data.GetString("key") + key := input.Key key = strings.Trim(key, " /") if len(key) == 0 { return nil, httperrors.NewInputParameterError("empty directory name") @@ -853,21 +870,20 @@ func (bucket *SBucket) AllowPerformDelete(ctx context.Context, return bucket.IsOwner(userCred) } +// 删除对象 +// +// 删除对象 func (bucket *SBucket) PerformDelete( ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, - data jsonutils.JSONObject, + input api.BucketPerformDeleteInput, ) (jsonutils.JSONObject, error) { if len(bucket.ExternalId) == 0 { return nil, httperrors.NewInvalidStatusError("no external bucket") } - keys, _ := data.Get("keys") - if keys == nil { - return nil, httperrors.NewInputParameterError("missing keys") - } - keyStrs := keys.(*jsonutils.JSONArray).GetStringArray() + keyStrs := input.Keys if len(keyStrs) == 0 { return nil, httperrors.NewInputParameterError("empty keys") } @@ -910,6 +926,9 @@ func (bucket *SBucket) AllowPerformUpload(ctx context.Context, return bucket.IsOwner(userCred) } +// 上传对象 +// +// 上传对象 func (bucket *SBucket) PerformUpload( ctx context.Context, userCred mcclient.TokenCredential, @@ -1030,6 +1049,9 @@ func (bucket *SBucket) AllowPerformAcl(ctx context.Context, return bucket.IsOwner(userCred) } +// 设置对象和bucket的ACL +// +// 设置对象和bucket的ACL func (bucket *SBucket) PerformAcl( ctx context.Context, userCred mcclient.TokenCredential, @@ -1082,6 +1104,8 @@ func (bucket *SBucket) AllowPerformSyncstatus(ctx context.Context, return bucket.IsOwner(userCred) } +// 同步存储桶状态 +// // 同步存储桶状态 func (bucket *SBucket) PerformSyncstatus( ctx context.Context, @@ -1157,23 +1181,27 @@ func (bucket *SBucket) AllowGetDetailsAcl( return bucket.IsOwner(userCred) } +// 获取对象或bucket的ACL +// +// 获取对象或bucket的ACL func (bucket *SBucket) GetDetailsAcl( ctx context.Context, userCred mcclient.TokenCredential, - query jsonutils.JSONObject, -) (jsonutils.JSONObject, error) { + input api.BucketGetAclInput, +) (api.BucketGetAclOutput, error) { + output := api.BucketGetAclOutput{} if len(bucket.ExternalId) == 0 { - return nil, httperrors.NewInvalidStatusError("no external bucket") + return output, httperrors.NewInvalidStatusError("no external bucket") } iBucket, err := bucket.GetIBucket() if err != nil { if errors.Cause(err) == httperrors.ErrInvalidStatus { - return nil, httperrors.NewInvalidStatusError("%s", err) + return output, httperrors.NewInvalidStatusError("%s", err) } else { - return nil, httperrors.NewInternalServerError("fail to find external bucket: %s", err) + return output, httperrors.NewInternalServerError("fail to find external bucket: %s", err) } } - objKey, _ := query.GetString("key") + objKey := input.Key var acl cloudprovider.TBucketACLType if len(objKey) == 0 { acl = iBucket.GetAcl() @@ -1181,16 +1209,15 @@ func (bucket *SBucket) GetDetailsAcl( object, err := cloudprovider.GetIObject(iBucket, objKey) if err != nil { if err == cloudprovider.ErrNotFound { - return nil, httperrors.NewNotFoundError("object %s not found", objKey) + return output, httperrors.NewNotFoundError("object %s not found", objKey) } else { - return nil, httperrors.NewInternalServerError("iBucket.GetIObjects error %s", err) + return output, httperrors.NewInternalServerError("iBucket.GetIObjects error %s", err) } } acl = object.GetAcl() } - ret := jsonutils.NewDict() - ret.Add(jsonutils.NewString(string(acl)), "acl") - return ret, nil + output.Acl = string(acl) + return output, nil } func (manager *SBucketManager) usageQByCloudEnv(q *sqlchemy.SQuery, providers []string, brands []string, cloudEnv string) *sqlchemy.SQuery { diff --git a/pkg/compute/models/guestcdrom.go b/pkg/compute/models/guestcdrom.go index 1076e1c5c9..266d89f94e 100644 --- a/pkg/compute/models/guestcdrom.go +++ b/pkg/compute/models/guestcdrom.go @@ -43,6 +43,7 @@ func init() { GuestcdromManager.SetVirtualObject(GuestcdromManager) } +// +onecloud:swagger-gen-ignore type SGuestcdrom struct { db.SModelBase diff --git a/pkg/compute/models/netinterfaces.go b/pkg/compute/models/netinterfaces.go index b8a0950d49..9e98e4b5a1 100644 --- a/pkg/compute/models/netinterfaces.go +++ b/pkg/compute/models/netinterfaces.go @@ -29,6 +29,7 @@ import ( "yunion.io/x/onecloud/pkg/util/rbacutils" ) +// +onecloud:swagger-gen-ignore type SNetInterface struct { db.SModelBase diff --git a/pkg/multicloud/azure/shell/network.go b/pkg/multicloud/azure/shell/network.go index 6740cd789b..024802bcd7 100644 --- a/pkg/multicloud/azure/shell/network.go +++ b/pkg/multicloud/azure/shell/network.go @@ -69,7 +69,7 @@ func init() { }) type NetworkInterfaceCreateOptions struct { - ResourceGroup string `help":"ResourceGroup Name"` + ResourceGroup string `help:"ResourceGroup Name"` NAME string `help:"Nic interface name"` IP string `help:"Nic private ip address"` NETWORK string `help:"Netowrk ID"`