-
Notifications
You must be signed in to change notification settings - Fork 2
/
meth_apideployment_deploy.go
108 lines (96 loc) · 3.64 KB
/
meth_apideployment_deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2020 Google LLC
//
// 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.
package gsu
import (
"fmt"
"log"
"strings"
"time"
"github.com/BrunoReboul/ram/utilities/str"
"google.golang.org/api/serviceusage/v1"
)
var activeAPIs []string
// Deploy activates APIs
func (apiDeployment *APIDeployment) Deploy() (err error) {
log.Printf("%s gsu APIs", apiDeployment.Core.InstanceName)
apiDeployment.Artifacts.ServicesService = apiDeployment.Core.Services.ServiceusageService.Services
apiDeployment.Artifacts.OperationsService = apiDeployment.Core.Services.ServiceusageService.Operations
activeAPIs = make([]string, 0)
parent := fmt.Sprintf("projects/%s", apiDeployment.Core.SolutionSettings.Hosting.ProjectID)
// log.Println(parent)
err = apiDeployment.Artifacts.ServicesService.List(parent).Filter("state:ENABLED").PageSize(200).Pages(apiDeployment.Core.Ctx, browseActiveAPIs)
if err != nil {
if strings.Contains(err.Error(), "403") {
log.Printf("%s gsu WARNING impossible to LIST APIs %v", apiDeployment.Core.InstanceName, err)
return nil
}
return fmt.Errorf("gsu ServicesService.List %v", err)
}
for _, apiName := range apiDeployment.Settings.Service.GSU.APIList {
if str.Find(activeAPIs, apiName) {
log.Printf("%s gsu API already active %s", apiDeployment.Core.InstanceName, apiName)
} else {
err = apiDeployment.activateAPI(apiName)
if err != nil {
return err
}
}
}
return nil
}
func browseActiveAPIs(listServicesResponse *serviceusage.ListServicesResponse) (err error) {
for _, googleAPIServiceusageV1Service := range listServicesResponse.Services {
parts := strings.Split(googleAPIServiceusageV1Service.Name, "/")
activeAPIs = append(activeAPIs, parts[len(parts)-1])
}
return nil
}
func (apiDeployment *APIDeployment) activateAPI(apiName string) (err error) {
name := fmt.Sprintf("projects/%s/services/%s", apiDeployment.Core.SolutionSettings.Hosting.ProjectID, apiName)
var request serviceusage.EnableServiceRequest
operation, err := apiDeployment.Artifacts.ServicesService.Enable(name, &request).Context(apiDeployment.Core.Ctx).Do()
if err != nil {
return err
}
log.Printf("%s gsu API %s activation started", apiDeployment.Core.InstanceName, apiName)
operationName := operation.Name
log.Println(operationName)
// Operation GET not working as expecter: returns 404 notFound
// for {
// time.Sleep(5 * time.Second)
// operation, err = apiDeployment.Artifacts.OperationsService.Get(operationName).Context(apiDeployment.Core.Ctx).Do()
// if err != nil {
// return err
// }
// if operation.Done {
// break
// }
// }
// ffo.JSONMarshalIndentPrint(operation)
// Work arround: check result, API activated
var googleAPIServiceusageV1Service *serviceusage.GoogleApiServiceusageV1Service
for {
time.Sleep(5 * time.Second)
googleAPIServiceusageV1Service, err = apiDeployment.Artifacts.ServicesService.Get(name).Context(apiDeployment.Core.Ctx).Do()
if err != nil {
return err
}
if googleAPIServiceusageV1Service.State == "ENABLED" {
break
}
}
// ffo.JSONMarshalIndentPrint(googleAPIServiceusageV1Service)
log.Printf("%s gsu API %s is active", apiDeployment.Core.InstanceName, apiName)
return nil
}