diff --git a/config/config.go b/config/config.go index 84c61d4..885ad9d 100644 --- a/config/config.go +++ b/config/config.go @@ -34,6 +34,14 @@ func NewConfig(cfgFile string) (*Config, error) { } type Config struct { + Organization string `toml:"organization"` + CloudletKey CloudletKey `toml:"cloudlet"` + Region string `toml:"region"` +} + +type CloudletKey struct { + Organization string `toml:"organization"` + Name string `toml:"name"` } func (c *Config) Validate() error { diff --git a/config/config.toml b/config/config.toml new file mode 100644 index 0000000..b4b0adb --- /dev/null +++ b/config/config.toml @@ -0,0 +1,6 @@ +organization = "edp-developer-framework" +region = "EU" + +[cloudlet] +name = "Munich" +organization = "TelekomOP" diff --git a/internal/client/client.go b/internal/client/client.go index 388e045..b72052d 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -141,6 +141,56 @@ func (e *EdgeConnect) ShowApp(ctx context.Context, appkey AppKey, region string) return response.Data, nil } +func (e *EdgeConnect) ShowAppInstance(ctx context.Context, appinstkey AppInstanceKey, region string) (AppInstance, error) { + token, err := e.RetrieveToken(ctx) + if err != nil { + return AppInstance{}, err + } + + input := struct { + App struct { + AppInstKey AppInstanceKey `json:"key"` + } `json:"appinst"` + Region string `json:"Region"` + }{ + App: struct { + AppInstKey AppInstanceKey `json:"key"` + }{ + AppInstKey: appinstkey, + }, + Region: region, + } + + json_data, err := json.Marshal(input) + if err != nil { + return AppInstance{}, err + } + + request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/ShowAppInst", bytes.NewBuffer(json_data)) + if err != nil { + return AppInstance{}, err + } + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) + + resp, err := e.HttpClient.Do(request) + if err != nil { + return AppInstance{}, err + } + + defer resp.Body.Close() + var response struct { + Data AppInstance `json:"data"` + } + + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + return AppInstance{}, err + } + + return response.Data, nil +} + func (e *EdgeConnect) RetrieveToken(ctx context.Context) (string, error) { json_data, err := json.Marshal(map[string]string{ "username": e.Credentials.Username, @@ -221,7 +271,7 @@ func (e *EdgeConnect) DeleteApp(ctx context.Context, appkey AppKey, region strin return nil } -func (e *EdgeConnect) DeleteAppInstance(ctx context.Context, appinstancekey AppInstanceKey, appkey AppKey, region string) error { +func (e *EdgeConnect) DeleteAppInstance(ctx context.Context, appinstancekey AppInstanceKey, region string) error { token, err := e.RetrieveToken(ctx) if err != nil { return err diff --git a/internal/client/models.go b/internal/client/models.go index f88d2b1..3a91b96 100644 --- a/internal/client/models.go +++ b/internal/client/models.go @@ -6,9 +6,11 @@ type NewAppInstanceInput struct { } type AppInstance struct { - Key AppInstanceKey `json:"key"` - AppKey AppKey `json:"app_key"` - Flavor Flavor `json:"flavor"` + Key AppInstanceKey `json:"key"` + AppKey AppKey `json:"app_key"` + Flavor Flavor `json:"flavor"` + State string `json:"state"` + PowerState string `json:"power_state"` } type AppInstanceKey struct { diff --git a/lala/lala.go b/lala/lala.go index 72ddc17..afd4237 100644 --- a/lala/lala.go +++ b/lala/lala.go @@ -7,6 +7,7 @@ import ( "os" "edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/internal/client" + "edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/provider" ) var testManifest = ` @@ -62,7 +63,7 @@ func main() { }, } - e.DeleteAppInstance(ctx, client.AppInstanceKey{ + /*e.DeleteAppInstance(ctx, client.AppInstanceKey{ Organization: "edp-developer-framework", Name: "mganterInstanceTest", CloudletKey: client.CloudletKey{ @@ -70,11 +71,7 @@ func main() { Name: "Munich", }, }, - client.AppKey{ - Organization: "edp-developer-framework", - Name: "mganter-test", - Version: "0.0.1", - }, "EU") + "EU") e.DeleteApp(ctx, client.AppKey{ Organization: "edp-developer-framework", @@ -134,6 +131,25 @@ func main() { }, }) + fmt.Printf("Error: %v\n", err)*/ + + appinst, err := e.ShowAppInstance(ctx, client.AppInstanceKey{ + Organization: "edp-developer-framework", + Name: "mganterInstanceTest", + CloudletKey: client.CloudletKey{ + Organization: "TelekomOP", + Name: "Munich", + }, + }, + "EU") + + fmt.Printf("appinst: %v\n", appinst) + fmt.Printf("Error: %v\n", err) + + edgeprovider, err := provider.NewEdgeConnectProvider("/home/chris/ipcei/projects/garm-provider-edge-connect/config/config.toml", "lalacontroller") + providerinst, err := edgeprovider.GetInstance(ctx, appinst.Key.Name) + + fmt.Printf("provider: %v\n", providerinst) fmt.Printf("Error: %v\n", err) /* diff --git a/provider/provider.go b/provider/provider.go index 4cc265f..1058be9 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -17,8 +17,11 @@ package provider import ( "context" "fmt" + "net/http" + "os" "edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/config" + "edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/internal/client" execution "github.com/cloudbase/garm-provider-common/execution/v0.1.0" "github.com/cloudbase/garm-provider-common/params" @@ -31,13 +34,25 @@ func NewEdgeConnectProvider(configPath, controllerID string) (execution.External if err != nil { return nil, fmt.Errorf("error loading config: %w", err) } + + client := client.EdgeConnect{ + BaseURL: "https://hub.apps.edge.platform.mg3.mdb.osc.live", + HttpClient: &http.Client{}, + Credentials: client.Credentials{ + Username: os.Getenv("EDGEXR_USERNAME"), + Password: os.Getenv("EDGEXR_PASSWORD"), + }, + } + return &edgeConnectProvider{ + client: client, controllerID: controllerID, cfg: conf, }, nil } type edgeConnectProvider struct { + client client.EdgeConnect controllerID string cfg *config.Config } @@ -65,13 +80,31 @@ func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance strin // GetInstance will return details about one instance. func (a *edgeConnectProvider) GetInstance(ctx context.Context, instance string) (params.ProviderInstance, error) { providerInstance := params.ProviderInstance{ - ProviderID: "", - Name: "", - OSType: "", - OSArch: "", - OSName: "", - OSVersion: "", - Status: "running", + ProviderID: a.controllerID, + Name: instance, + OSType: params.Linux, + OSArch: params.Amd64, + OSName: "lala", + OSVersion: "lalatest", + Status: params.InstanceStatusUnknown, + } + + appinstkey := client.AppInstanceKey{ + Organization: a.cfg.Organization, + Name: instance, + CloudletKey: client.CloudletKey{ + Organization: a.cfg.CloudletKey.Organization, + Name: a.cfg.CloudletKey.Name, + }, + } + + appinst, err := a.client.ShowAppInstance(ctx, appinstkey, a.cfg.Region) + if err != nil { + return params.ProviderInstance{}, err + } + + if appinst.State == "Ready" { + providerInstance.Status = params.InstanceRunning } return providerInstance, nil