/*
Copyright 2021 The Rook Authors. All rights reserved.

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 installer

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"path"
	"time"

	"github.com/pkg/errors"
	"github.com/rook/rook/tests/framework/utils"
)

func readManifest(provider, filename string) string {
	rootDir, err := utils.FindRookRoot()
	if err != nil {
		panic(err)
	}
	manifest := path.Join(rootDir, "cluster/examples/kubernetes/", provider, filename)
	logger.Infof("Reading manifest: %s", manifest)
	contents, err := ioutil.ReadFile(manifest)
	if err != nil {
		panic(errors.Wrapf(err, "failed to read manifest at %s", manifest))
	}
	return string(contents)
}

func readManifestFromGithub(rookVersion, provider, filename string) string {
	url := fmt.Sprintf("https://raw.githubusercontent.com/rook/rook/%s/cluster/examples/kubernetes/%s/%s", rookVersion, provider, filename)
	logger.Infof("Retrieving manifest: %s", url)
	var response *http.Response
	var err error
	for i := 1; i <= 3; i++ {
		// #nosec G107 This is only test code and is expected to read from a url
		response, err = http.Get(url)
		if err != nil {
			if i == 3 {
				panic(errors.Wrapf(err, "failed to read manifest from %s", url))
			}
			logger.Warningf("failed to read manifest from %s. retrying in 1sec. %v", url, err)
			time.Sleep(time.Second)
			continue
		}
		break
	}
	defer response.Body.Close()

	content, err := ioutil.ReadAll(response.Body)
	if err != nil {
		panic(errors.Wrapf(err, "failed to read content from %s", url))
	}
	return string(content)
}
