Skip to content
Permalink
Browse files
Merge pull request #53 from svanharmelen/f-project-support
Add better support for using projects with the helper functions
  • Loading branch information
Sander van Harmelen committed Apr 21, 2016
2 parents 6e28220 + 218dc09 commit cfbfb481e04c131cb89df1c6141b082f2714bc29
Showing 65 changed files with 1,135 additions and 1,087 deletions.
@@ -1,5 +1,5 @@
//
// Copyright 2014, Sander van Harmelen
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
//
// Copyright 2014, Sander van Harmelen
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -1130,12 +1130,18 @@ func (s *AccountService) NewListAccountsParams() *ListAccountsParams {
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AccountService) GetAccountID(name string) (string, error) {
func (s *AccountService) GetAccountID(name string, opts ...OptionFunc) (string, error) {
p := &ListAccountsParams{}
p.p = make(map[string]interface{})

p.p["name"] = name

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return "", err
}
}

l, err := s.ListAccounts(p)
if err != nil {
return "", err
@@ -1160,26 +1166,32 @@ func (s *AccountService) GetAccountID(name string) (string, error) {
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AccountService) GetAccountByName(name string) (*Account, int, error) {
id, err := s.GetAccountID(name)
func (s *AccountService) GetAccountByName(name string, opts ...OptionFunc) (*Account, int, error) {
id, err := s.GetAccountID(name, opts...)
if err != nil {
return nil, -1, err
}

r, count, err := s.GetAccountByID(id)
r, count, err := s.GetAccountByID(id, opts...)
if err != nil {
return nil, count, err
}
return r, count, nil
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AccountService) GetAccountByID(id string) (*Account, int, error) {
func (s *AccountService) GetAccountByID(id string, opts ...OptionFunc) (*Account, int, error) {
p := &ListAccountsParams{}
p.p = make(map[string]interface{})

p.p["id"] = id

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return nil, -1, err
}
}

l, err := s.ListAccounts(p)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf(
@@ -1716,28 +1728,24 @@ func (s *AccountService) NewListProjectAccountsParams(projectid string) *ListPro
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AccountService) GetProjectAccountID(keyword string, projectid string) (string, error) {
func (s *AccountService) GetProjectAccountID(keyword string, projectid string, opts ...OptionFunc) (string, error) {
p := &ListProjectAccountsParams{}
p.p = make(map[string]interface{})

p.p["keyword"] = keyword
p.p["projectid"] = projectid

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return "", err
}
}

l, err := s.ListProjectAccounts(p)
if err != nil {
return "", err
}

if l.Count == 0 {
// If no matches, search all projects
p.p["projectid"] = "-1"

l, err = s.ListProjectAccounts(p)
if err != nil {
return "", err
}
}

if l.Count == 0 {
return "", fmt.Errorf("No match found for %s: %+v", keyword, l)
}
@@ -1,5 +1,5 @@
//
// Copyright 2014, Sander van Harmelen
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -581,12 +581,18 @@ func (s *AddressService) NewListPublicIpAddressesParams() *ListPublicIpAddresses
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AddressService) GetPublicIpAddressByID(id string) (*PublicIpAddress, int, error) {
func (s *AddressService) GetPublicIpAddressByID(id string, opts ...OptionFunc) (*PublicIpAddress, int, error) {
p := &ListPublicIpAddressesParams{}
p.p = make(map[string]interface{})

p.p["id"] = id

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return nil, -1, err
}
}

l, err := s.ListPublicIpAddresses(p)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf(
@@ -597,21 +603,6 @@ func (s *AddressService) GetPublicIpAddressByID(id string) (*PublicIpAddress, in
return nil, -1, err
}

if l.Count == 0 {
// If no matches, search all projects
p.p["projectid"] = "-1"

l, err = s.ListPublicIpAddresses(p)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf(
"Invalid parameter id value=%s due to incorrect long value format, "+
"or entity does not exist", id)) {
return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
}
return nil, -1, err
}
}

if l.Count == 0 {
return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
}
@@ -1,5 +1,5 @@
//
// Copyright 2014, Sander van Harmelen
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -427,27 +427,23 @@ func (s *AffinityGroupService) NewListAffinityGroupsParams() *ListAffinityGroups
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AffinityGroupService) GetAffinityGroupID(name string) (string, error) {
func (s *AffinityGroupService) GetAffinityGroupID(name string, opts ...OptionFunc) (string, error) {
p := &ListAffinityGroupsParams{}
p.p = make(map[string]interface{})

p.p["name"] = name

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return "", err
}
}

l, err := s.ListAffinityGroups(p)
if err != nil {
return "", err
}

if l.Count == 0 {
// If no matches, search all projects
p.p["projectid"] = "-1"

l, err = s.ListAffinityGroups(p)
if err != nil {
return "", err
}
}

if l.Count == 0 {
return "", fmt.Errorf("No match found for %s: %+v", name, l)
}
@@ -467,26 +463,32 @@ func (s *AffinityGroupService) GetAffinityGroupID(name string) (string, error) {
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AffinityGroupService) GetAffinityGroupByName(name string) (*AffinityGroup, int, error) {
id, err := s.GetAffinityGroupID(name)
func (s *AffinityGroupService) GetAffinityGroupByName(name string, opts ...OptionFunc) (*AffinityGroup, int, error) {
id, err := s.GetAffinityGroupID(name, opts...)
if err != nil {
return nil, -1, err
}

r, count, err := s.GetAffinityGroupByID(id)
r, count, err := s.GetAffinityGroupByID(id, opts...)
if err != nil {
return nil, count, err
}
return r, count, nil
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AffinityGroupService) GetAffinityGroupByID(id string) (*AffinityGroup, int, error) {
func (s *AffinityGroupService) GetAffinityGroupByID(id string, opts ...OptionFunc) (*AffinityGroup, int, error) {
p := &ListAffinityGroupsParams{}
p.p = make(map[string]interface{})

p.p["id"] = id

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return nil, -1, err
}
}

l, err := s.ListAffinityGroups(p)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf(
@@ -497,21 +499,6 @@ func (s *AffinityGroupService) GetAffinityGroupByID(id string) (*AffinityGroup,
return nil, -1, err
}

if l.Count == 0 {
// If no matches, search all projects
p.p["projectid"] = "-1"

l, err = s.ListAffinityGroups(p)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf(
"Invalid parameter id value=%s due to incorrect long value format, "+
"or entity does not exist", id)) {
return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
}
return nil, -1, err
}
}

if l.Count == 0 {
return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
}
@@ -1,5 +1,5 @@
//
// Copyright 2014, Sander van Harmelen
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -113,12 +113,18 @@ func (s *AlertService) NewListAlertsParams() *ListAlertsParams {
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AlertService) GetAlertID(name string) (string, error) {
func (s *AlertService) GetAlertID(name string, opts ...OptionFunc) (string, error) {
p := &ListAlertsParams{}
p.p = make(map[string]interface{})

p.p["name"] = name

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return "", err
}
}

l, err := s.ListAlerts(p)
if err != nil {
return "", err
@@ -143,26 +149,32 @@ func (s *AlertService) GetAlertID(name string) (string, error) {
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AlertService) GetAlertByName(name string) (*Alert, int, error) {
id, err := s.GetAlertID(name)
func (s *AlertService) GetAlertByName(name string, opts ...OptionFunc) (*Alert, int, error) {
id, err := s.GetAlertID(name, opts...)
if err != nil {
return nil, -1, err
}

r, count, err := s.GetAlertByID(id)
r, count, err := s.GetAlertByID(id, opts...)
if err != nil {
return nil, count, err
}
return r, count, nil
}

// This is a courtesy helper function, which in some cases may not work as expected!
func (s *AlertService) GetAlertByID(id string) (*Alert, int, error) {
func (s *AlertService) GetAlertByID(id string, opts ...OptionFunc) (*Alert, int, error) {
p := &ListAlertsParams{}
p.p = make(map[string]interface{})

p.p["id"] = id

for _, fn := range opts {
if err := fn(s.cs, p); err != nil {
return nil, -1, err
}
}

l, err := s.ListAlerts(p)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf(
@@ -1,5 +1,5 @@
//
// Copyright 2014, Sander van Harmelen
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
//
// Copyright 2014, Sander van Harmelen
// Copyright 2016, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

0 comments on commit cfbfb48

Please sign in to comment.