<commit_before>/*
   Copyright 2020 Docker, Inc.

   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 azure

import (
	"context"

	"github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
	"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
	"github.com/pkg/errors"

	"github.com/docker/api/errdefs"
)

// ACIResourceGroupHelper interface to manage resource groups and subscription IDs
type ACIResourceGroupHelper interface {
	GetSubscriptionIDs(ctx context.Context) ([]subscription.Model, error)
	ListGroups(ctx context.Context, subscriptionID string) ([]resources.Group, error)
	GetGroup(ctx context.Context, subscriptionID string, groupName string) (resources.Group, error)
	CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error)
	Delete(ctx context.Context, subscriptionID string, resourceGroupName string) error
}

type aciResourceGroupHelperImpl struct {
}

// NewACIResourceGroupHelper create a new ACIResourceGroupHelper
func NewACIResourceGroupHelper() ACIResourceGroupHelper {
	return aciResourceGroupHelperImpl{}
}

// GetGroup get a resource group from its name
func (mgt aciResourceGroupHelperImpl) GetGroup(ctx context.Context, subscriptionID string, groupName string) (resources.Group, error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return resources.Group{}, err
	}
	return gc.Get(ctx, groupName)
}

// ListGroups list resource groups
func (mgt aciResourceGroupHelperImpl) ListGroups(ctx context.Context, subscriptionID string) ([]resources.Group, error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return nil, err
	}

	groupResponse, err := gc.List(ctx, "", nil)
	if err != nil {
		return nil, err
	}

	groups := groupResponse.Values()
	return groups, nil
}

// CreateOrUpdate create or update a resource group
func (mgt aciResourceGroupHelperImpl) CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return resources.Group{}, err
	}
	return gc.CreateOrUpdate(ctx, resourceGroupName, parameters)
}

// Delete deletes a resource group
func (mgt aciResourceGroupHelperImpl) Delete(ctx context.Context, subscriptionID string, resourceGroupName string) (err error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return err<commit_msg>List pages of resource groups if entire list is not fetch in first page<commit_after>/*
   Copyright 2020 Docker, Inc.

   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 azure

import (
	"context"

	"github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
	"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
	"github.com/pkg/errors"

	"github.com/docker/api/errdefs"
)

// ACIResourceGroupHelper interface to manage resource groups and subscription IDs
type ACIResourceGroupHelper interface {
	GetSubscriptionIDs(ctx context.Context) ([]subscription.Model, error)
	ListGroups(ctx context.Context, subscriptionID string) ([]resources.Group, error)
	GetGroup(ctx context.Context, subscriptionID string, groupName string) (resources.Group, error)
	CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error)
	Delete(ctx context.Context, subscriptionID string, resourceGroupName string) error
}

type aciResourceGroupHelperImpl struct {
}

// NewACIResourceGroupHelper create a new ACIResourceGroupHelper
func NewACIResourceGroupHelper() ACIResourceGroupHelper {
	return aciResourceGroupHelperImpl{}
}

// GetGroup get a resource group from its name
func (mgt aciResourceGroupHelperImpl) GetGroup(ctx context.Context, subscriptionID string, groupName string) (resources.Group, error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return resources.Group{}, err
	}
	return gc.Get(ctx, groupName)
}

// ListGroups list resource groups
func (mgt aciResourceGroupHelperImpl) ListGroups(ctx context.Context, subscriptionID string) ([]resources.Group, error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return nil, err
	}

	groupResponse, err := gc.List(ctx, "", nil)
	if err != nil {
		return nil, err
	}

	groups := groupResponse.Values()

	for groupResponse.NotDone() {
		err = groupResponse.NextWithContext(ctx)
		if err != nil {
			return nil, err
		}
		newValues := groupResponse.Values()
		groups = append(groups, newValues...)
	}

	return groups, nil
}

// CreateOrUpdate create or update a resource group
func (mgt aciResourceGroupHelperImpl) CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return resources.Group{}, err
	}
	return gc.CreateOrUpdate(ctx, resourceGroupName, parameters)
}

// Delete deletes a resource group
func (mgt aciResourceGroupHelperImpl) Delete(ctx context.Context, subscriptionID string, resourceGroupName string) (err error) {
	gc, err := getGroupsClient(subscriptionID)
	if err != nil {
		return err
{'fraction_non_alphanumeric': 0.08898706216472073, 'fraction_numerical': 0.0050489113284947935, 'mean_word_length': 5.048664122137405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5248854', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1768, 'n_words': 587}