mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-30 17:13:08 +08:00
cloudcommon: add ProxySetting
This commit is contained in:
@@ -0,0 +1 @@
|
||||
package proxy // import "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy"
|
||||
@@ -0,0 +1,36 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
)
|
||||
|
||||
const (
|
||||
ProxySettingId_DIRECT = "DIRECT"
|
||||
)
|
||||
|
||||
type ProxySettingCreateInput struct {
|
||||
apis.VirtualResourceCreateInput
|
||||
|
||||
HttpProxy string
|
||||
HttpsProxy string
|
||||
NoProxy string
|
||||
}
|
||||
|
||||
type ProxySettingUpdateInput ProxySettingCreateInput
|
||||
|
||||
// String implements ISerializable interface
|
||||
func (ps *SProxySetting) String() string {
|
||||
return jsonutils.Marshal(ps).String()
|
||||
}
|
||||
|
||||
// IsZero implements ISerializable interface
|
||||
func (ps *SProxySetting) IsZero() bool {
|
||||
if ps.HTTPProxy == "" &&
|
||||
ps.HTTPSProxy == "" &&
|
||||
ps.NoProxy == "" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
// Code generated by model-api-gen. DO NOT EDIT.
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
)
|
||||
|
||||
// SProxySetting is an autogenerated struct via yunion.io/x/onecloud/pkg/cloudcommon/db/proxy.SProxySetting.
|
||||
type SProxySetting struct {
|
||||
apis.SStandaloneResourceBase
|
||||
HTTPProxy string `json:"http_proxy"`
|
||||
HTTPSProxy string `json:"https_proxy"`
|
||||
NoProxy string `json:"no_proxy"`
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package proxy // import "yunion.io/x/onecloud/pkg/cloudcommon/db/proxy"
|
||||
@@ -0,0 +1,90 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/http/httpproxy"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
type SProxySettingManager struct {
|
||||
db.SStandaloneResourceBaseManager
|
||||
}
|
||||
|
||||
var ProxySettingManager *SProxySettingManager
|
||||
|
||||
func init() {
|
||||
ProxySettingManager = &SProxySettingManager{
|
||||
SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager(
|
||||
SProxySetting{},
|
||||
"proxysettings_tbl",
|
||||
"proxysetting",
|
||||
"proxysettings",
|
||||
),
|
||||
}
|
||||
ProxySettingManager.SetVirtualObject(ProxySettingManager)
|
||||
}
|
||||
|
||||
type SProxySetting struct {
|
||||
db.SStandaloneResourceBase
|
||||
|
||||
HTTPProxy string `create:"admin_optional" list:"admin" update:"admin"`
|
||||
HTTPSProxy string `create:"admin_optional" list:"admin" update:"admin"`
|
||||
NoProxy string `create:"admin_optional" list:"admin" update:"admin"`
|
||||
}
|
||||
|
||||
func (man *SProxySettingManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data proxyapi.ProxySettingCreateInput) (proxyapi.ProxySettingCreateInput, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (ps *SProxySetting) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data proxyapi.ProxySettingUpdateInput) (proxyapi.ProxySettingUpdateInput, error) {
|
||||
if ps.Id == proxyapi.ProxySettingId_DIRECT {
|
||||
return data, httperrors.NewConflictError("DIRECT setting cannot be changed")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (ps *SProxySetting) HttpTransportProxyFunc() httputils.TransportProxyFunc {
|
||||
cfg := &httpproxy.Config{
|
||||
HTTPProxy: ps.HTTPProxy,
|
||||
HTTPSProxy: ps.HTTPSProxy,
|
||||
NoProxy: ps.NoProxy,
|
||||
}
|
||||
proxyFunc := cfg.ProxyFunc()
|
||||
return func(req *http.Request) (*url.URL, error) {
|
||||
return proxyFunc(req.URL)
|
||||
}
|
||||
}
|
||||
|
||||
func (man *SProxySettingManager) InitializeData() error {
|
||||
_, err := man.FetchById(proxyapi.ProxySettingId_DIRECT)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
|
||||
m, err := db.NewModelObject(man)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ps := m.(*SProxySetting)
|
||||
ps.Id = proxyapi.ProxySettingId_DIRECT
|
||||
ps.Name = proxyapi.ProxySettingId_DIRECT
|
||||
ps.Description = "Connect directly"
|
||||
if err := man.TableSpec().Insert(ps); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -124,6 +124,7 @@ class ModelAPI(FuncDispatcher):
|
||||
|
||||
def gen_cloudcommon(self):
|
||||
self.run(pkg=["cloudcommon", "db"])
|
||||
self.run(pkg=["cloudcommon", "db", "proxy"], out=["cloudcommon", "proxy"])
|
||||
|
||||
def gen_cloudprovider(self):
|
||||
self.run_same("cloudprovider")
|
||||
|
||||
Reference in New Issue
Block a user