Skip to content

Commit

Permalink
Update the GetAllByGroupName method
Browse files Browse the repository at this point in the history
1. Why is this change necessary?
Because if an error occurs there is no way to handle it in the log.Fatal
handle of the error

2. How does it address the issue?
Returns the error to the caller, so is easy to handle the error

3. What side effects does this change have?
None in the use of the project, but does in the testing. Accordingly
with: golang/go#31859 we need a
`testing.Init()` in the `init` function of the testing file

4. Test
`make test ARGS="-client edae63d9-3f66-47de-860d-8b23b55c8544 -tenant c8cd0425-e7b7-4f3d-9215-7e5fa3f439e8 -secret 1016e85d-3f23-4953-97a1-2cec4dccb0a4"`
  • Loading branch information
pitakill committed Sep 19, 2019
1 parent 5c7d726 commit 5a6d66b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -12,12 +12,12 @@ go-generate:

go-test:
@echo " > Testing"
go test -race .
go test -race . $(ARGS)

go-test-clean:
@echo " > Cleaning test"
@go clean -testcache

go-test-verbose:
@echo " > Testing verbosely"
go test -v . -race
go test -v -race . $(ARGS)
2 changes: 1 addition & 1 deletion compute.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions generate/main.tpl
Expand Up @@ -5,7 +5,6 @@ package azure_resources

import (
"context"
"log"

"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources"
"github.com/Azure/go-autorest/autorest"
Expand All @@ -15,20 +14,20 @@ import (

var authorizer autorest.Authorizer

type resource interface {
type Resource interface {
GetProperties() ([]byte, error)
}

func GetAllByGroupName(subscriptionID, groupName string) []resource {
func GetAllByGroupName(subscriptionID, groupName string) ([]Resource, error) {
client := resources.NewClient(subscriptionID)
client.Authorizer = authorizer
results, err := client.ListByResourceGroup(context.Background(), groupName, "", "", nil)
if err != nil {
log.Fatalln(err)
return nil, err
}

var resources []resource
var resources []Resource

for _, resource := range results.Values() {
switch *resource.Type {
Expand All @@ -45,7 +44,7 @@ func GetAllByGroupName(subscriptionID, groupName string) []resource {
}
}

return resources
return resources, nil
}

func SetAuthorizer(tenantID, clientID, clientSecret string) error {
Expand Down
9 changes: 4 additions & 5 deletions main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 27 additions & 3 deletions main_test.go
@@ -1,14 +1,38 @@
package azure_resources

import "testing"
import (
"flag"
"testing"
)

var (
clientID string
clientSecret string
tenantID string
)

func init() {
testing.Init()
flag.StringVar(&clientID, "client", "", "client to make the petition")
flag.StringVar(&clientSecret, "secret", "", "secret to make the petition")
flag.StringVar(&tenantID, "tenant", "", "tenant to make the petition")
flag.Parse()
}

func TestGetAllByGroupName(t *testing.T) {
const (
subscriptionID = "72166d64-f454-46c1-a2f7-e84b57df44b8"
groupName = "azure-test"
groupName = "terraform-resources"
)

resources := GetAllByGroupName(subscriptionID, groupName)
if err := SetAuthorizer(tenantID, clientID, clientSecret); err != nil {
t.Error(err)
}

resources, err := GetAllByGroupName(subscriptionID, groupName)
if err != nil {
t.Error(err)
}

for _, r := range resources {
props, err := r.GetProperties()
Expand Down
2 changes: 1 addition & 1 deletion network.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5a6d66b

Please sign in to comment.