Skip to content

Commit 240c81d

Browse files
authored
Jayin/moreexamples (#92)
* follow golang example naming standards * add retry logic * clean up resources and add object storage sample * split the pagination sample * update launch instance output * clean up VCN after created * add sample for raw request * forgot remove the ocid * add document about how to run samples * fix a typo
1 parent 68ce461 commit 240c81d

14 files changed

+1005
-117
lines changed

common/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func defaultHTTPDispatcher() http.Client {
8383

8484
func defaultBaseClient(provider KeyProvider) BaseClient {
8585
dispatcher := defaultHTTPDispatcher()
86-
signer := defaultRequestSigner(provider)
86+
signer := DefaultRequestSigner(provider)
8787
return newBaseClient(signer, &dispatcher)
8888
}
8989

common/http_signer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ var (
5050
}
5151
)
5252

53-
func defaultRequestSigner(provider KeyProvider) HTTPRequestSigner {
53+
// DefaultRequestSigner creates a signer with default parameters.
54+
func DefaultRequestSigner(provider KeyProvider) HTTPRequestSigner {
5455
return RequestSigner(provider, defaultGenericHeaders, defaultBodyHeaders)
5556
}
5657

example/.env.sample

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# The Availability Domain of the instance. Example: Uocm:PHX-AD-1
2+
# OCI_AVAILABILITY_DOMAIN=Uocm:PHX-AD-1
3+
OCI_AVAILABILITY_DOMAIN=
4+
5+
# The OCID of the compartment.
6+
# OCI_COMPARTMENT_ID=ocid1.compartment.oc1..thisisafakeocidaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
7+
OCI_COMPARTMENT_ID=
8+
9+
# The OCID of the root compartment.
10+
# OCI_ROOT_COMPARTMENT_ID=ocidv1:tenancy:oc1:phx:00000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
11+
OCI_ROOT_COMPARTMENT_ID=

example/example_audit_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
2+
//
3+
// Example code for Audit API
4+
//
5+
package example
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"time"
11+
12+
"github.com/oracle/oci-go-sdk/audit"
13+
"github.com/oracle/oci-go-sdk/common"
14+
"github.com/oracle/oci-go-sdk/example/helpers"
15+
)
16+
17+
func ExampleListEvents() {
18+
c, clerr := audit.NewAuditClientWithConfigurationProvider(common.DefaultConfigProvider())
19+
helpers.LogIfError(clerr)
20+
21+
// list events for last 5 hour
22+
req := audit.ListEventsRequest{
23+
CompartmentId: helpers.CompartmentID(),
24+
StartTime: &common.SDKTime{time.Now().Add(time.Hour * -5)},
25+
EndTime: &common.SDKTime{time.Now()},
26+
}
27+
28+
_, err := c.ListEvents(context.Background(), req)
29+
helpers.LogIfError(err)
30+
31+
//log.Printf("events returned back: %v", resp.Items)
32+
fmt.Println("list events completed")
33+
34+
// Output:
35+
// list events completed
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
2+
//
3+
// Example code for Core Services API
4+
//
5+
package example
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"log"
11+
12+
"github.com/oracle/oci-go-sdk/common"
13+
"github.com/oracle/oci-go-sdk/core"
14+
"github.com/oracle/oci-go-sdk/example/helpers"
15+
)
16+
17+
// ExampleListShapes_Pagination demostrate how to use page parameter
18+
func ExampleListShapes_Pagination() {
19+
c, err := core.NewComputeClientWithConfigurationProvider(common.DefaultConfigProvider())
20+
helpers.LogIfError(err)
21+
22+
request := core.ListShapesRequest{
23+
CompartmentId: helpers.CompartmentID(),
24+
}
25+
26+
// to show how pagination works, reduce number of items to return in a paginated "List" call
27+
request.Limit = common.Int(2)
28+
29+
listShapesFunc := func(request core.ListShapesRequest) (core.ListShapesResponse, error) {
30+
return c.ListShapes(context.Background(), request)
31+
}
32+
33+
for r, err := listShapesFunc(request); ; r, err = listShapesFunc(request) {
34+
helpers.LogIfError(err)
35+
36+
log.Printf("list shapes returns: %v", r.Items)
37+
38+
if r.OpcNextPage != nil {
39+
// if there are more items in next page, fetch items from next page
40+
request.Page = r.OpcNextPage
41+
} else {
42+
// no more result, break the loop
43+
break
44+
}
45+
}
46+
47+
fmt.Println("list shapes completed")
48+
49+
// Output:
50+
// list shapes completed
51+
}

0 commit comments

Comments
 (0)