diff --git a/pkg/apis/cloudcommon/proxy/doc.go b/pkg/apis/cloudcommon/proxy/doc.go new file mode 100644 index 0000000000..5ed87c1edc --- /dev/null +++ b/pkg/apis/cloudcommon/proxy/doc.go @@ -0,0 +1 @@ +package proxy // import "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy" diff --git a/pkg/apis/cloudcommon/proxy/proxy.go b/pkg/apis/cloudcommon/proxy/proxy.go new file mode 100644 index 0000000000..6f4f619b86 --- /dev/null +++ b/pkg/apis/cloudcommon/proxy/proxy.go @@ -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 +} diff --git a/pkg/apis/cloudcommon/proxy/zz_generated.model.go b/pkg/apis/cloudcommon/proxy/zz_generated.model.go new file mode 100644 index 0000000000..ee143b6f38 --- /dev/null +++ b/pkg/apis/cloudcommon/proxy/zz_generated.model.go @@ -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"` +} diff --git a/pkg/cloudcommon/db/proxy/doc.go b/pkg/cloudcommon/db/proxy/doc.go new file mode 100644 index 0000000000..b5fd7e2ca8 --- /dev/null +++ b/pkg/cloudcommon/db/proxy/doc.go @@ -0,0 +1 @@ +package proxy // import "yunion.io/x/onecloud/pkg/cloudcommon/db/proxy" diff --git a/pkg/cloudcommon/db/proxy/proxysetting.go b/pkg/cloudcommon/db/proxy/proxysetting.go new file mode 100644 index 0000000000..e466c4389b --- /dev/null +++ b/pkg/cloudcommon/db/proxy/proxysetting.go @@ -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 +} diff --git a/scripts/codegen.py b/scripts/codegen.py index d873e35bb5..896127a802 100755 --- a/scripts/codegen.py +++ b/scripts/codegen.py @@ -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")