<commit_before>// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.

package oci

import (
	"context"

	"github.com/hashicorp/terraform/helper/schema"
	oci_database "github.com/oracle/oci-go-sdk/database"
)

func DatabaseBackupDestinationsDataSource() *schema.Resource {
	return &schema.Resource{
		Read: readDatabaseBackupDestinations,
		Schema: map[string]*schema.Schema{
			"filter": dataSourceFiltersSchema(),
			"compartment_id": {
				Type:     schema.TypeString,
				Required: true,
			},
			"type": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"backup_destinations": {
				Type:     schema.TypeList,
				Computed: true,
				Elem:     GetDataSourceItemSchema(DatabaseBackupDestinationResource()),
			},
		},
	}
}

func readDatabaseBackupDestinations(d *schema.ResourceData, m interface{}) error {
	sync := &DatabaseBackupDestinationsDataSourceCrud{}
	sync.D = d
	sync.Client = m.(*OracleClients).databaseClient

	return ReadResource(sync)
}

type DatabaseBackupDestinationsDataSourceCrud struct {
	D      *schema.ResourceData
	Client *oci_database.DatabaseClient
	Res    *oci_database.ListBackupDestinationResponse
}

func (s *DatabaseBackupDestinationsDataSourceCrud) VoidState() {
	s.D.SetId("")
}

func (s *DatabaseBackupDestinationsDataSourceCrud) Get() error {
	request := oci_database.ListBackupDestinationRequest{}

	if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
		tmp := compartmentId.(string)
		request.CompartmentId = &tmp
	}

	if type_, ok := s.D.GetOkExists("type"); ok {
		tmp := type_.(string)
		request.Type = &tmp
	}

	request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "database")

	response, err := s.Client.ListBackupDestination(context.Background(), request)
	if err != nil {
		return err
	}

	s.Res = &response
	if s.Res != nil && s.Res.RawResponse != nil {
		rawResponse := s.Res.RawResponse
		nextPage := rawResponse.Header.Get(OpcNextPageHeader)
		request.Page = &nextPage
		for request.Page != nil && *request.Page != "" {
			listResponse, err := s.Client.ListBackupDestination(context.Background(), request)
			if err != nil {
				return err
			}
			s.Res.Items = append(s.Res.Items, listResponse.Items...)
			if listResponse.RawResponse != nil {
				nextPage = listResponse.RawResponse.Header.Get(OpcNextPageHeader)
				request.Page = &nextPage
			} else {
				request.Page = nil
			}
		}
	}

	return nil
}

func (s *DatabaseBackupDestinationsDataSourceCrud) SetData() error {
	if s.Res == nil {<commit_msg>Fix next page logic in backup destinations list data source<commit_after>// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.

package oci

import (
	"context"

	"github.com/hashicorp/terraform/helper/schema"
	oci_database "github.com/oracle/oci-go-sdk/database"
)

func DatabaseBackupDestinationsDataSource() *schema.Resource {
	return &schema.Resource{
		Read: readDatabaseBackupDestinations,
		Schema: map[string]*schema.Schema{
			"filter": dataSourceFiltersSchema(),
			"compartment_id": {
				Type:     schema.TypeString,
				Required: true,
			},
			"type": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"backup_destinations": {
				Type:     schema.TypeList,
				Computed: true,
				Elem:     GetDataSourceItemSchema(DatabaseBackupDestinationResource()),
			},
		},
	}
}

func readDatabaseBackupDestinations(d *schema.ResourceData, m interface{}) error {
	sync := &DatabaseBackupDestinationsDataSourceCrud{}
	sync.D = d
	sync.Client = m.(*OracleClients).databaseClient

	return ReadResource(sync)
}

type DatabaseBackupDestinationsDataSourceCrud struct {
	D      *schema.ResourceData
	Client *oci_database.DatabaseClient
	Res    *oci_database.ListBackupDestinationResponse
}

func (s *DatabaseBackupDestinationsDataSourceCrud) VoidState() {
	s.D.SetId("")
}

func (s *DatabaseBackupDestinationsDataSourceCrud) Get() error {
	request := oci_database.ListBackupDestinationRequest{}

	if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
		tmp := compartmentId.(string)
		request.CompartmentId = &tmp
	}

	if type_, ok := s.D.GetOkExists("type"); ok {
		tmp := type_.(string)
		request.Type = &tmp
	}

	request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "database")

	response, err := s.Client.ListBackupDestination(context.Background(), request)
	if err != nil {
		return err
	}

	s.Res = &response
	request.Page = s.Res.OpcNextPage

	for request.Page != nil {
		listResponse, err := s.Client.ListBackupDestination(context.Background(), request)
		if err != nil {
			return err
		}

		s.Res.Items = append(s.Res.Items, listResponse.Items...)
		request.Page = listResponse.OpcNextPage
	}

	return nil
}

func (s *DatabaseBackupDestinationsDataSourceCrud) SetData() error {
	if s.Res == nil {
{'fraction_non_alphanumeric': 0.12167539267015706, 'fraction_numerical': 0.003350785340314136, 'mean_word_length': 4.29490022172949, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '246276', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1534, 'n_words': 313}