Skip to content

Commit

Permalink
Add appblb bcc cert eip vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed Oct 12, 2019
1 parent 90629f4 commit 0868fe1
Show file tree
Hide file tree
Showing 57 changed files with 15,507 additions and 85 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ bce-sdk-go
|--bce //BCE公用基础组件
|--http //BCE的http通信模块
|--services //BCE相关服务目录
| |--appblb //应用型负载均衡服务目录
| |--bcc //云服务器
| |--bie //百度边缘计算
| |--bos //BOS服务目录
| | |--bos_client.go //BOS客户端入口
| | |--api //BOS相关API目录
Expand All @@ -36,9 +39,13 @@ bce-sdk-go
| | |--multipart.go //BOS的Multipart相关API实现
| | |--module.go //BOS相关API的数据模型
| | |--util.go //BOS相关API实现使用的工具
| |--cdn //内容分布网络
| |--cert //SSL证书服务
| |--eip //弹性公网IP
| |--sts //STS服务目录
| |--vca //VCA服务目录
| |--vcr //VCR服务目录
| |--vpc //私有网络
|--util //BCE公用的工具实现
```

Expand Down Expand Up @@ -210,9 +217,14 @@ myLogger.Info("this is my own logger from the sdk")
产品名称 | 产品缩写 | 导入路径 | 说明文档
-----------|----------|----------|----------
应用型负载均衡 | APPBLB | github.com/baidubce/bce-sdk-go/services/appblb |
云服务器 | BCC | github.com/baidubce/bce-sdk-go/services/bcc |
百度边缘计算 | BIE | github.com/baidubce/bce-sdk-go/services/bie |
百度对象存储 | BOS | github.com/baidubce/bce-sdk-go/services/bos | [BOS.md](./doc/BOS.md)
内容分布网络 | CDN | github.com/baidubce/bce-sdk-go/services/cdn | [CDN.md](./doc/CDN.md)
SSL证书服务 | CERT | github.com/baidubce/bce-sdk-go/services/cert |
弹性公网IP | EIP | github.com/baidubce/bce-sdk-go/services/eip |
安全Token服务| STS | github.com/baidubce/bce-sdk-go/services/sts | [STS.md](./doc/STS.md)
视频内容分析 | VCA | github.com/baidubce/bce-sdk-go/services/vca |
视频内容审核 | VCR | github.com/baidubce/bce-sdk-go/services/vcr |
百度边缘计算 | BIE | github.com/baidubce/bce-sdk-go/services/bie |
内容分布网络 | CDN | github.com/baidubce/bce-sdk-go/services/cdn | [CDN.md](./doc/CDN.md)
私有网络 | VPC | github.com/baidubce/bce-sdk-go/services/vpc |
189 changes: 189 additions & 0 deletions bce/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Copyright 2017 Baidu, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

// builder.go - defines the RequestBuilder structure for BCE servies

package bce

import (
"encoding/json"
"fmt"
)

// RequestBuilder holds config data for bce request.
// Some of fields are required and the others are optional.
// The builder pattern can simplify the execution of requests.
type RequestBuilder struct {
client Client

url string // required
method string // required
queryParams map[string]string // optional
headers map[string]string // optional
body interface{} // optional
result interface{} // optional
}

// create RequestBuilder with the client.
func NewRequestBuilder(client Client) *RequestBuilder {
return &RequestBuilder{
client: client,
}
}

func (b *RequestBuilder) WithURL(url string) *RequestBuilder {
b.url = url
return b
}

func (b *RequestBuilder) WithMethod(method string) *RequestBuilder {
b.method = method
return b
}

// set query param with the key/value directly.
func (b *RequestBuilder) WithQueryParam(key, value string) *RequestBuilder {
if b.queryParams == nil {
b.queryParams = make(map[string]string)
}
b.queryParams[key] = value
return b
}

// set query param with the key/value only when the value is not blank.
func (b *RequestBuilder) WithQueryParamFilter(key, value string) *RequestBuilder {
if len(value) == 0 {
return b
}
return b.WithQueryParam(key, value)
}

func (b *RequestBuilder) WithQueryParams(params map[string]string) *RequestBuilder {
if b.queryParams == nil {
b.queryParams = params
} else {
for key, value := range params {
b.queryParams[key] = value
}
}
return b
}

func (b *RequestBuilder) WithHeader(key, value string) *RequestBuilder {
if b.headers == nil {
b.headers = make(map[string]string)
}
b.headers[key] = value
return b
}

func (b *RequestBuilder) WithHeaders(headers map[string]string) *RequestBuilder {
if b.headers == nil {
b.headers = headers
} else {
for key, value := range headers {
b.headers[key] = value
}
}
return b
}

func (b *RequestBuilder) WithBody(body interface{}) *RequestBuilder {
b.body = body
return b
}

func (b *RequestBuilder) WithResult(result interface{}) *RequestBuilder {
b.result = result
return b
}

// Do will send request to bce and get result with the builder's parameters.
func (b *RequestBuilder) Do() error {
if err := b.validate(); err != nil {
return err
}

// build BceRequest
req, err := b.buildBceRequest()
if err != nil {
return err
}

// get result from BceResponse
if err := b.buildBceResponse(req); err != nil {
return err
}

return nil
}

// Validate if the required fields are providered.
func (b *RequestBuilder) validate() error {
if len(b.url) == 0 {
return fmt.Errorf("The url can't be null.")
}
if len(b.method) == 0 {
return fmt.Errorf("The method can't be null.")
}
if b.client == nil {
return fmt.Errorf("The client can't be null.")
}
return nil
}

func (b *RequestBuilder) buildBceRequest() (*BceRequest, error) {
// Build the request
req := &BceRequest{}
req.SetUri(b.url)
req.SetMethod(b.method)

if b.headers != nil {
req.SetHeaders(b.headers)
}
if b.queryParams != nil {
req.SetParams(b.queryParams)
}
if b.body != nil {
bodyBytes, err := json.Marshal(b.body)
if err != nil {
return nil, err
}
body, err := NewBodyFromBytes(bodyBytes)
if err != nil {
return nil, err
}
req.SetBody(body)
}

return req, nil
}

func (b *RequestBuilder) buildBceResponse(req *BceRequest) error {
// Send request and get response
resp := &BceResponse{}
if err := b.client.SendRequest(req, resp); err != nil {
return err
}
if resp.IsFail() {
return resp.ServiceError()
}
defer resp.Body().Close()

if b.result == nil {
return nil
}

return resp.ParseJsonBody(b.result)
}
21 changes: 21 additions & 0 deletions bce/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,24 @@ func (c *BceClient) SendRequest(req *BceRequest, resp *BceResponse) error {
func NewBceClient(conf *BceClientConfiguration, sign auth.Signer) *BceClient {
return &BceClient{conf, sign}
}

func NewBceClientWithAkSk(ak, sk, endPoint string) (*BceClient, error) {
credentials, err := auth.NewBceCredentials(ak, sk)
if err != nil {
return nil, err
}
defaultSignOptions := &auth.SignOptions{
HeadersToSign: auth.DEFAULT_HEADERS_TO_SIGN,
ExpireSeconds: auth.DEFAULT_EXPIRE_SECONDS}
defaultConf := &BceClientConfiguration{
Endpoint: endPoint,
Region: DEFAULT_REGION,
UserAgent: DEFAULT_USER_AGENT,
Credentials: credentials,
SignOption: defaultSignOptions,
Retry: DEFAULT_RETRY_POLICY,
ConnectionTimeoutInMillis: DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS}
v1Signer := &auth.BceV1Signer{}

return NewBceClient(defaultConf, v1Signer), nil
}
Loading

0 comments on commit 0868fe1

Please sign in to comment.