Skip to content

Commit e07029c

Browse files
committed
feat(destination) add -di option
* Support `-di` flag for `html5-list` command * Support `-di` flag for `html5-push` command * Support `-di` flag for `html5-delete` command * Fix Name of `app-host` service instance
1 parent 9ad6f55 commit e07029c

13 files changed

+428
-120
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [1.4.5] - ???
10+
### Added
11+
- Support `-di` flag for `html5-list` command ([#43](https://github.com/SAP/cf-html5-apps-repo-cli-plugin/issues/43))
12+
- Support `-di` flag for `html5-push` command ([#44](https://github.com/SAP/cf-html5-apps-repo-cli-plugin/issues/44))
13+
- Support `-di` flag for `html5-delete` command ([#45](https://github.com/SAP/cf-html5-apps-repo-cli-plugin/issues/45))
14+
15+
### Fixed
16+
- Requests to Destination configuration service now have the correct `Content-Type` header value (`application/json`)
17+
- Name of `app-host` service instance in case `sap.cloud/service` is not set in `manifest.json`
18+
919
## [1.4.4] - 2020-10-19
1020
### Added
1121
- Support new business service destination configuration format in `html5-list -d` and

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
NAME=cf-html5-apps-repo-cli-plugin
2-
VERSION=1.4.4
2+
VERSION=1.4.5
33

44
# Build the project
55
all: clean build install

README.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The CF HTML5 Applications Repository CLI Plugin supports the following commands:
8282

8383
| Version | Changes |
8484
|----------|---------------------------------------------|
85+
| `v1.4.5` | The `--destination-instance` option added |
8586
| `v1.4.0` | The `--destination` option added |
8687
| `v1.3.0` | The `--name` option added |
8788
| `v1.1.0` | The `--url` option added |
@@ -94,27 +95,32 @@ NAME:
9495
html5-list - Display list of HTML5 applications or file paths of specified application
9596
9697
USAGE:
97-
cf html5-list [APP_NAME] [APP_VERSION] [APP_HOST_ID|-n APP_HOST_NAME] [-d|-a CF_APP_NAME [-u]]
98+
cf html5-list [APP_NAME] [APP_VERSION] [APP_HOST_ID|-n APP_HOST_NAME]
99+
[-d|-di DESTINATION_SERVICE_INSTANCE_NAME|-a CF_APP_NAME [-u]]
98100
99101
OPTIONS:
100-
-APP_NAME Application name, which file paths should be listed.
101-
If not provided, list of applications will be printed.
102-
-APP_VERSION Application version, which file paths should be listed.
103-
If not provided, the current active version will be used.
104-
-APP_HOST_ID GUID of html5-apps-repo app-host service instance that
105-
contains application with specified name and version
106-
-APP_HOST_NAME Name of html5-apps-repo app-host service instance that
107-
contains the application with specified name and version.
108-
--name, -n Use html5-apps-repo app-host service instance name
109-
instead of APP_HOST_ID
110-
--destination, -d List HTML5 applications exposed via destinations with
111-
sap.cloud.service and html5-apps-repo.app_host_id
112-
properties
113-
--app, -a Cloud Foundry application name, which is bound to
114-
services that expose UI via html5-apps-repo
115-
--url, -u Show conventional URLs of the applications, when accessed
116-
via Cloud Foundry application specified with --app flag
117-
or when --destination flag is used
102+
-APP_NAME Application name, which file paths should be listed.
103+
If not provided, list of applications will be printed.
104+
-APP_VERSION Application version, which file paths should be listed.
105+
If not provided, the current active version will be used.
106+
-APP_HOST_ID GUID of html5-apps-repo app-host service instance that
107+
contains application with specified name and version
108+
-APP_HOST_NAME Name of html5-apps-repo app-host service instance that
109+
contains the application with specified name and version.
110+
-DESTINATION_SERVICE_INSTANCE_NAME Name of destination service intance
111+
--name, -n Use html5-apps-repo app-host service instance name
112+
instead of APP_HOST_ID
113+
--destination, -d List HTML5 applications exposed via destinations with
114+
sap.cloud.service and html5-apps-repo.app_host_id
115+
properties
116+
--destination-instance, -di List HTML5 applications exposed via service instance
117+
destinations with sap.cloud.service and
118+
html5-apps-repo.app_host_id properties
119+
--app, -a Cloud Foundry application name, which is bound to
120+
services that expose UI via html5-apps-repo
121+
--url, -u Show conventional URLs of the applications, when accessed
122+
via Cloud Foundry application specified with --app flag
123+
or when --destination flag is used
118124
```
119125

120126
#### html5-get
@@ -159,6 +165,7 @@ OPTIONS:
159165

160166
| Version | Changes |
161167
|----------|---------------------------------------------------|
168+
| `v1.4.5` | The `--destination-instance` option added |
162169
| `v1.4.0` | The `--destination` and `--service` options added |
163170
| `v1.2.0` | The `--name` and `--redeploy` options added |
164171
| `v1.0.0` | Added in `v1.0.0` |
@@ -170,7 +177,8 @@ NAME:
170177
html5-push - Push HTML5 applications to html5-apps-repo service
171178
172179
USAGE:
173-
cf html5-push [-d|-s SERVICE_INSTANCE_NAME] [-r|-n APP_HOST_NAME] [PATH_TO_APP_FOLDER ...] [APP_HOST_ID]
180+
cf html5-push [-d|-di DESTINATION_SERVICE_INSTANCE_NAME|-s SERVICE_INSTANCE_NAME] [-r|-n APP_HOST_NAME]
181+
[PATH_TO_APP_FOLDER ...] [APP_HOST_ID]
174182
175183
OPTIONS:
176184
-APP_HOST_ID GUID of html5-apps-repo app-host service instance
@@ -196,6 +204,7 @@ OPTIONS:
196204

197205
| Version | Changes |
198206
|----------|---------------------------------------------|
207+
| `v1.4.5` | The `--destination-instance` option added |
199208
| `v1.4.0` | The `--destination` option added |
200209
| `v1.3.0` | The `--name` option added |
201210
| `v1.1.0` | Added in `v1.1.0` |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package clients
2+
3+
import (
4+
"bytes"
5+
models "cf-html5-apps-repo-cli-plugin/clients/models"
6+
"cf-html5-apps-repo-cli-plugin/log"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
)
11+
12+
// CreateServiceInstanceDestination create destination service service instance destination
13+
func CreateServiceInstanceDestination(serviceURL string, accessToken string, destination models.DestinationConfiguration) error {
14+
var err error
15+
var request *http.Request
16+
var response *http.Response
17+
var destinationsURL string
18+
var payload []byte
19+
var body []byte
20+
21+
log.Tracef("Marshaling destination configuration: %+v\n", destination)
22+
payload, err = destination.MarshalJSON()
23+
if err != nil {
24+
return err
25+
}
26+
log.Tracef("Destination configuration JSON: %s\n", payload)
27+
28+
destinationsURL = serviceURL + "/destination-configuration/v1/instanceDestinations/"
29+
log.Tracef("Making request to: %s\n", destinationsURL)
30+
request, err = http.NewRequest("POST", destinationsURL, bytes.NewBuffer(payload))
31+
if err != nil {
32+
return err
33+
}
34+
request.Header.Set("Content-Type", "application/json")
35+
request.Header.Set("Authorization", "Bearer "+accessToken)
36+
37+
client := &http.Client{}
38+
response, err = client.Do(request)
39+
if err != nil {
40+
return err
41+
}
42+
defer response.Body.Close()
43+
44+
if response.StatusCode > 201 {
45+
body, err = ioutil.ReadAll(response.Body)
46+
if err != nil {
47+
return fmt.Errorf("Could not create destination: [%s] %+v", response.Status, body)
48+
}
49+
}
50+
51+
return nil
52+
}

clients/create_subaccount_destination.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func CreateSubaccountDestination(serviceURL string, accessToken string, destinat
3131
if err != nil {
3232
return err
3333
}
34-
request.Header.Set("Content-Type", "appication/json")
34+
request.Header.Set("Content-Type", "application/json")
3535
request.Header.Set("Authorization", "Bearer "+accessToken)
3636

3737
client := &http.Client{}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package clients
2+
3+
import (
4+
"bytes"
5+
"cf-html5-apps-repo-cli-plugin/log"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
)
10+
11+
// DeleteServiceInstanceDestination delete destination service instance level destination
12+
func DeleteServiceInstanceDestination(serviceURL string, accessToken string, destinationName string) error {
13+
var err error
14+
var request *http.Request
15+
var response *http.Response
16+
var destinationsURL string
17+
var payload = []byte{}
18+
var body []byte
19+
20+
destinationsURL = serviceURL + "/destination-configuration/v1/instanceDestinations/" + destinationName
21+
log.Tracef("Making request to: %s\n", destinationsURL)
22+
request, err = http.NewRequest("DELETE", destinationsURL, bytes.NewBuffer(payload))
23+
if err != nil {
24+
return err
25+
}
26+
request.Header.Set("Content-Type", "application/json")
27+
request.Header.Set("Authorization", "Bearer "+accessToken)
28+
29+
client := &http.Client{}
30+
response, err = client.Do(request)
31+
if err != nil {
32+
return err
33+
}
34+
defer response.Body.Close()
35+
36+
if response.StatusCode > 201 {
37+
body, err = ioutil.ReadAll(response.Body)
38+
if err != nil {
39+
return fmt.Errorf("Could not delete destination: [%s] %+v", response.Status, body)
40+
}
41+
}
42+
43+
return nil
44+
}

clients/delete_subaccount_destination.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func DeleteSubaccountDestination(serviceURL string, accessToken string, destinat
2323
if err != nil {
2424
return err
2525
}
26-
request.Header.Set("Content-Type", "appication/json")
26+
request.Header.Set("Content-Type", "application/json")
2727
request.Header.Set("Authorization", "Bearer "+accessToken)
2828

2929
client := &http.Client{}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package clients
2+
3+
import (
4+
models "cf-html5-apps-repo-cli-plugin/clients/models"
5+
"cf-html5-apps-repo-cli-plugin/log"
6+
"encoding/json"
7+
"io/ioutil"
8+
"net/http"
9+
)
10+
11+
// ListServiceInstanceDestinations list destination service instance destinations
12+
func ListServiceInstanceDestinations(serviceURL string, accessToken string) (models.DestinationListDestinationsResponse, error) {
13+
var destinations models.DestinationListDestinationsResponse
14+
var request *http.Request
15+
var response *http.Response
16+
var err error
17+
var destinationsURL string
18+
var body []byte
19+
20+
destinationsURL = serviceURL + "/destination-configuration/v1/instanceDestinations/"
21+
22+
log.Tracef("Making request to: %s\n", destinationsURL)
23+
24+
client := &http.Client{}
25+
request, err = http.NewRequest("GET", destinationsURL, nil)
26+
if err != nil {
27+
return destinations, err
28+
}
29+
request.Header.Add("Authorization", "Bearer "+accessToken)
30+
response, err = client.Do(request)
31+
if err != nil {
32+
return destinations, err
33+
}
34+
35+
// Get response body
36+
defer response.Body.Close()
37+
body, err = ioutil.ReadAll(response.Body)
38+
if err != nil {
39+
return destinations, err
40+
}
41+
42+
// Parse response JSON
43+
err = json.Unmarshal(body, &destinations)
44+
if err != nil {
45+
return destinations, err
46+
}
47+
48+
return destinations, nil
49+
}

commands/html5_base_command.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (c *HTML5Command) Dispose(name string) {
5252
}
5353

5454
// GetDestinationContext get destination context
55-
func (c *HTML5Command) GetDestinationContext(context Context) (DestinationContext, error) {
55+
func (c *HTML5Command) GetDestinationContext(context Context, destinationInstanceName string) (DestinationContext, error) {
5656

5757
// Context to return
5858
var destinationContext = DestinationContext{}
@@ -108,12 +108,30 @@ func (c *HTML5Command) GetDestinationContext(context Context) (DestinationContex
108108
}
109109
destinationContext.DestinationServiceInstances = destinationServiceInstances
110110

111+
// Sort destination service instance so that the requested instance to be the first one in the list.
112+
// If specific destinaton service instance name is required, but not found - return error
113+
if destinationInstanceName != "" {
114+
found := false
115+
for idx, instance := range destinationServiceInstances {
116+
if instance.Name == destinationInstanceName {
117+
tmp := destinationServiceInstances[0]
118+
destinationServiceInstances[0] = instance
119+
destinationServiceInstances[idx] = tmp
120+
found = true
121+
break
122+
}
123+
}
124+
if !found {
125+
return destinationContext, fmt.Errorf("Could not find service instance of 'destination' service 'lite' plan with name '%s'", destinationInstanceName)
126+
}
127+
}
128+
111129
// Create instance of 'lite' plan if needed
112130
if len(destinationServiceInstances) == 0 {
113131
log.Tracef("Creating service instance of 'destination' service 'lite' plan\n")
114132
destinationServiceInstance, err := clients.CreateServiceInstance(c.CliConnection, context.SpaceID, *liteServicePlan, nil, "")
115133
if err != nil {
116-
return destinationContext, fmt.Errorf("Could not create service service instance of 'destination' service 'lite' plan: %s", err.Error())
134+
return destinationContext, fmt.Errorf("Could not create service instance of 'destination' service 'lite' plan: %s", err.Error())
117135
}
118136
destinationServiceInstances = append(destinationServiceInstances, *destinationServiceInstance)
119137
destinationContext.DestinationServiceInstance = destinationServiceInstance
@@ -123,10 +141,10 @@ func (c *HTML5Command) GetDestinationContext(context Context) (DestinationContex
123141

124142
// Create service key
125143
log.Tracef("Creating service key for 'destination' service 'lite' plan\n")
126-
destinationServiceInstanceKey, err := clients.CreateServiceKey(c.CliConnection, destinationServiceInstances[len(destinationServiceInstances)-1].GUID)
144+
destinationServiceInstanceKey, err := clients.CreateServiceKey(c.CliConnection, destinationServiceInstances[0].GUID)
127145
if err != nil {
128146
return destinationContext, fmt.Errorf("Could not create service key of %s service instance: %s",
129-
destinationServiceInstances[len(destinationServiceInstances)-1].Name,
147+
destinationServiceInstances[0].Name,
130148
err.Error())
131149
}
132150
destinationContext.DestinationServiceInstanceKey = destinationServiceInstanceKey

0 commit comments

Comments
 (0)