diff options
Diffstat (limited to 'vnfs/DAaaS/microservices/visualization-operator/pkg')
9 files changed, 643 insertions, 0 deletions
diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/addtoscheme_onap_v1alpha1.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/addtoscheme_onap_v1alpha1.go new file mode 100644 index 00000000..57e6df97 --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/addtoscheme_onap_v1alpha1.go @@ -0,0 +1,10 @@ +package apis + +import ( + "visualization-operator/pkg/apis/onap/v1alpha1" +) + +func init() { + // Register the types with the Scheme so the components can map objects to GroupVersionKinds and back + AddToSchemes = append(AddToSchemes, v1alpha1.SchemeBuilder.AddToScheme) +} diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/group.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/group.go new file mode 100644 index 00000000..d35634ca --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/group.go @@ -0,0 +1,6 @@ +// Package onap contains onap API versions. +// +// This file ensures Go source parsers acknowledge the onap package +// and any child packages. It can be removed if any other Go source files are +// added to this package. +package onap diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/doc.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/doc.go new file mode 100644 index 00000000..29b3493e --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/doc.go @@ -0,0 +1,4 @@ +// Package v1alpha1 contains API Schema definitions for the onap v1alpha1 API group +// +k8s:deepcopy-gen=package,register +// +groupName=onap.org +package v1alpha1 diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/grafanadatasource_types.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/grafanadatasource_types.go new file mode 100644 index 00000000..60820067 --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/grafanadatasource_types.go @@ -0,0 +1,68 @@ +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + +// GrafanaDataSourceSpec defines the desired state of GrafanaDataSource +// +k8s:openapi-gen=true +type GrafanaDataSourceSpec struct { + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + // Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file + // Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html + Datasources []Datasource `json:"datasources"` + Grafana map[string]string `json:"grafana"` +} + +// GrafanaDataSourceStatus defines the observed state of GrafanaDataSource +// +k8s:openapi-gen=true +type GrafanaDataSourceStatus struct { +} + +// Datasource defines the fields in a GrafanaDataSource +// +k8s:openapi-gen=true +type Datasource struct { + Name string `json:"name"` + Type string `json:"type"` + URL string `json:"url"` + IsDefault bool `json:"isDefault,omitempty"` + Access string `json:"access"` + WithCredentials bool `json:"withCredentials,omitempty"` + BasicAuth bool `json:"basicAuth,omitempty"` + BasicAuthUser string `json:"basicAuthUser,omitempty"` + BasicAuthPassword string `json:"basicAuthPassword,omitempty"` + JsonData map[string]string `json:"jsonData,omitempty"` + Database string `json:"database,omitempty"` + User string `json:"user,omitempty"` + Password string `json:"password,omitempty"` + ReadOnly bool `json:"readOnly,omitempty"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// GrafanaDataSource is the Schema for the grafanadatasources API +// +k8s:openapi-gen=true +// +kubebuilder:subresource:status +type GrafanaDataSource struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec GrafanaDataSourceSpec `json:"spec,omitempty"` + Status GrafanaDataSourceStatus `json:"status,omitempty"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// GrafanaDataSourceList contains a list of GrafanaDataSource +type GrafanaDataSourceList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []GrafanaDataSource `json:"items"` +} + +func init() { + SchemeBuilder.Register(&GrafanaDataSource{}, &GrafanaDataSourceList{}) +} diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/register.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/register.go new file mode 100644 index 00000000..c670eea4 --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/register.go @@ -0,0 +1,19 @@ +// NOTE: Boilerplate only. Ignore this file. + +// Package v1alpha1 contains API Schema definitions for the onap v1alpha1 API group +// +k8s:deepcopy-gen=package,register +// +groupName=onap.org +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/runtime/scheme" +) + +var ( + // SchemeGroupVersion is group version used to register these objects + SchemeGroupVersion = schema.GroupVersion{Group: "onap.org", Version: "v1alpha1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} +) diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 00000000..2cc830ba --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,139 @@ +// +build !ignore_autogenerated + +// Code generated by operator-sdk. DO NOT EDIT. + +package v1alpha1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Datasource) DeepCopyInto(out *Datasource) { + *out = *in + if in.JsonData != nil { + in, out := &in.JsonData, &out.JsonData + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Datasource. +func (in *Datasource) DeepCopy() *Datasource { + if in == nil { + return nil + } + out := new(Datasource) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GrafanaDataSource) DeepCopyInto(out *GrafanaDataSource) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + out.Status = in.Status + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GrafanaDataSource. +func (in *GrafanaDataSource) DeepCopy() *GrafanaDataSource { + if in == nil { + return nil + } + out := new(GrafanaDataSource) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *GrafanaDataSource) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GrafanaDataSourceList) DeepCopyInto(out *GrafanaDataSourceList) { + *out = *in + out.TypeMeta = in.TypeMeta + out.ListMeta = in.ListMeta + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]GrafanaDataSource, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GrafanaDataSourceList. +func (in *GrafanaDataSourceList) DeepCopy() *GrafanaDataSourceList { + if in == nil { + return nil + } + out := new(GrafanaDataSourceList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *GrafanaDataSourceList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GrafanaDataSourceSpec) DeepCopyInto(out *GrafanaDataSourceSpec) { + *out = *in + if in.Datasources != nil { + in, out := &in.Datasources, &out.Datasources + *out = make([]Datasource, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Grafana != nil { + in, out := &in.Grafana, &out.Grafana + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GrafanaDataSourceSpec. +func (in *GrafanaDataSourceSpec) DeepCopy() *GrafanaDataSourceSpec { + if in == nil { + return nil + } + out := new(GrafanaDataSourceSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GrafanaDataSourceStatus) DeepCopyInto(out *GrafanaDataSourceStatus) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GrafanaDataSourceStatus. +func (in *GrafanaDataSourceStatus) DeepCopy() *GrafanaDataSourceStatus { + if in == nil { + return nil + } + out := new(GrafanaDataSourceStatus) + in.DeepCopyInto(out) + return out +} diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go new file mode 100644 index 00000000..7f74f2f7 --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/apis/onap/v1alpha1/zz_generated.openapi.go @@ -0,0 +1,220 @@ +// +build ! + +// This file was autogenerated by openapi-gen. Do not edit it manually! + +package v1alpha1 + +import ( + spec "github.com/go-openapi/spec" + common "k8s.io/kube-openapi/pkg/common" +) + +func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition { + return map[string]common.OpenAPIDefinition{ + "./pkg/apis/onap/v1alpha1.Datasource": schema_pkg_apis_onap_v1alpha1_Datasource(ref), + "./pkg/apis/onap/v1alpha1.GrafanaDataSource": schema_pkg_apis_onap_v1alpha1_GrafanaDataSource(ref), + "./pkg/apis/onap/v1alpha1.GrafanaDataSourceSpec": schema_pkg_apis_onap_v1alpha1_GrafanaDataSourceSpec(ref), + "./pkg/apis/onap/v1alpha1.GrafanaDataSourceStatus": schema_pkg_apis_onap_v1alpha1_GrafanaDataSourceStatus(ref), + } +} + +func schema_pkg_apis_onap_v1alpha1_Datasource(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "Datasource defines the fields in a GrafanaDataSource", + Properties: map[string]spec.Schema{ + "name": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "type": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "url": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "isDefault": { + SchemaProps: spec.SchemaProps{ + Type: []string{"boolean"}, + Format: "", + }, + }, + "access": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "withCredentials": { + SchemaProps: spec.SchemaProps{ + Type: []string{"boolean"}, + Format: "", + }, + }, + "basicAuth": { + SchemaProps: spec.SchemaProps{ + Type: []string{"boolean"}, + Format: "", + }, + }, + "basicAuthUser": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "basicAuthPassword": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "jsonData": { + SchemaProps: spec.SchemaProps{ + Type: []string{"object"}, + AdditionalProperties: &spec.SchemaOrBool{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + }, + }, + }, + "database": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "user": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "password": { + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + "readOnly": { + SchemaProps: spec.SchemaProps{ + Type: []string{"boolean"}, + Format: "", + }, + }, + }, + Required: []string{"name", "type", "url", "access"}, + }, + }, + Dependencies: []string{}, + } +} + +func schema_pkg_apis_onap_v1alpha1_GrafanaDataSource(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "GrafanaDataSource is the Schema for the grafanadatasources API", + Properties: map[string]spec.Schema{ + "kind": { + SchemaProps: spec.SchemaProps{ + Description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", + Type: []string{"string"}, + Format: "", + }, + }, + "apiVersion": { + SchemaProps: spec.SchemaProps{ + Description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources", + Type: []string{"string"}, + Format: "", + }, + }, + "metadata": { + SchemaProps: spec.SchemaProps{ + Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"), + }, + }, + "spec": { + SchemaProps: spec.SchemaProps{ + Ref: ref("./pkg/apis/onap/v1alpha1.GrafanaDataSourceSpec"), + }, + }, + "status": { + SchemaProps: spec.SchemaProps{ + Ref: ref("./pkg/apis/onap/v1alpha1.GrafanaDataSourceStatus"), + }, + }, + }, + }, + }, + Dependencies: []string{ + "./pkg/apis/onap/v1alpha1.GrafanaDataSourceSpec", "./pkg/apis/onap/v1alpha1.GrafanaDataSourceStatus", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"}, + } +} + +func schema_pkg_apis_onap_v1alpha1_GrafanaDataSourceSpec(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "GrafanaDataSourceSpec defines the desired state of GrafanaDataSource", + Properties: map[string]spec.Schema{ + "datasources": { + SchemaProps: spec.SchemaProps{ + Description: "INSERT ADDITIONAL SPEC FIELDS - desired state of cluster Important: Run \"operator-sdk generate k8s\" to regenerate code after modifying this file Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html", + Type: []string{"array"}, + Items: &spec.SchemaOrArray{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Ref: ref("./pkg/apis/onap/v1alpha1.Datasource"), + }, + }, + }, + }, + }, + "grafana": { + SchemaProps: spec.SchemaProps{ + Type: []string{"object"}, + AdditionalProperties: &spec.SchemaOrBool{ + Schema: &spec.Schema{ + SchemaProps: spec.SchemaProps{ + Type: []string{"string"}, + Format: "", + }, + }, + }, + }, + }, + }, + Required: []string{"datasources", "grafana"}, + }, + }, + Dependencies: []string{ + "./pkg/apis/onap/v1alpha1.Datasource"}, + } +} + +func schema_pkg_apis_onap_v1alpha1_GrafanaDataSourceStatus(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "GrafanaDataSourceStatus defines the observed state of GrafanaDataSource", + Properties: map[string]spec.Schema{}, + }, + }, + Dependencies: []string{}, + } +} diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/controller/add_grafanadatasource.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/controller/add_grafanadatasource.go new file mode 100644 index 00000000..350b8a06 --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/controller/add_grafanadatasource.go @@ -0,0 +1,10 @@ +package controller + +import ( + "visualization-operator/pkg/controller/grafanadatasource" +) + +func init() { + // AddToManagerFuncs is a list of functions to create controllers and add them to a manager. + AddToManagerFuncs = append(AddToManagerFuncs, grafanadatasource.Add) +} diff --git a/vnfs/DAaaS/microservices/visualization-operator/pkg/controller/grafanadatasource/grafanadatasource_controller.go b/vnfs/DAaaS/microservices/visualization-operator/pkg/controller/grafanadatasource/grafanadatasource_controller.go new file mode 100644 index 00000000..f46cf1b4 --- /dev/null +++ b/vnfs/DAaaS/microservices/visualization-operator/pkg/controller/grafanadatasource/grafanadatasource_controller.go @@ -0,0 +1,167 @@ +package grafanadatasource + +import ( + "bytes" + "context" + "encoding/json" + + onapv1alpha1 "visualization-operator/pkg/apis/onap/v1alpha1" + + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/runtime" + "net/http" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/manager" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" + "sigs.k8s.io/controller-runtime/pkg/source" +) + +var log = logf.Log.WithName("controller_grafanadatasource") + +// Add creates a new GrafanaDataSource Controller and adds it to the Manager. The Manager will set fields on the Controller +// and Start it when the Manager is Started. +func Add(mgr manager.Manager) error { + return add(mgr, newReconciler(mgr)) +} + +// newReconciler returns a new reconcile.Reconciler +func newReconciler(mgr manager.Manager) reconcile.Reconciler { + return &ReconcileGrafanaDataSource{client: mgr.GetClient(), scheme: mgr.GetScheme()} +} + +// add adds a new Controller to mgr with r as the reconcile.Reconciler +func add(mgr manager.Manager, r reconcile.Reconciler) error { + // Create a new controller + c, err := controller.New("grafanadatasource-controller", mgr, controller.Options{Reconciler: r}) + if err != nil { + return err + } + + // Watch for changes to primary resource GrafanaDataSource + err = c.Watch(&source.Kind{Type: &onapv1alpha1.GrafanaDataSource{}}, &handler.EnqueueRequestForObject{}) + if err != nil { + return err + } + + return nil +} + +// blank assignment to verify that ReconcileGrafanaDataSource implements reconcile.Reconciler +var _ reconcile.Reconciler = &ReconcileGrafanaDataSource{} + +// ReconcileGrafanaDataSource reconciles a GrafanaDataSource object +type ReconcileGrafanaDataSource struct { + // This client, initialized using mgr.Client() above, is a split client + // that reads objects from the cache and writes to the apiserver + client client.Client + scheme *runtime.Scheme +} + +// Reconcile reads that state of the cluster for a GrafanaDataSource object and makes changes based on the state read +// and what is in the GrafanaDataSource.Spec +// Note: +// The Controller will requeue the Request to be processed again if the returned error is non-nil or +// Result.Requeue is true, otherwise upon completion it will remove the work from the queue. +func (r *ReconcileGrafanaDataSource) Reconcile(request reconcile.Request) (reconcile.Result, error) { + reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) + reqLogger.Info("Reconciling GrafanaDataSource") + + // Fetch the GrafanaDataSource instance + instance := &onapv1alpha1.GrafanaDataSource{} + err := r.client.Get(context.TODO(), request.NamespacedName, instance) + if err != nil { + if errors.IsNotFound(err) { + // Request object not found, could have been deleted after reconcile request. + // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. + // Return and don't requeue + return reconcile.Result{}, nil + } + // Error reading the object - requeue the request. + + return reconcile.Result{}, err + } + + datasources := instance.Spec.Datasources + grafana := instance.Spec.Grafana + + reqLogger.V(1).Info(" Datasource Name ", "datasources", datasources) + + //loop through all datasources in the spec + for _, datasource := range datasources { + + //check if datasource exists + grafanaURL := grafana["url"] + "/api/datasources/name/" + datasource.Name + grafanaUsername := grafana["username"] + grafanaPassword := grafana["password"] + + client := &http.Client{} + req, err := http.NewRequest("GET", grafanaURL, nil) + if err != nil { + reqLogger.Error(err, "GET REQUEST error") + return reconcile.Result{}, err + } + req.SetBasicAuth(grafanaUsername, grafanaPassword) + getResp, err := client.Do(req) + if err != nil { + reqLogger.Error(err, "GET RESPONSE error") + return reconcile.Result{}, err + } + + defer getResp.Body.Close() + + //add datasource if datasource does not exist already + if getResp.StatusCode == http.StatusNotFound { + reqLogger.Info("Datasource does not exist, creating one...") + // create datasource + if err := createDataSource(grafana, datasource); err != nil { + return reconcile.Result{}, err + } + } else if getResp.StatusCode == http.StatusOK { + //if datasource already exists + reqLogger.V(1).Info("datasource already exists", "datasource", datasource.Name) + } else { + reqLogger.Error(err, "unknown error", datasource.Name) + } + + } + return reconcile.Result{}, nil +} + +func createDataSource(grafana map[string]string, datasource onapv1alpha1.Datasource) error { + reqLogger := log.WithValues("Datasource name", datasource.Name, "Datasource URL", datasource.URL) + reqLogger.Info("creating datasource") + + grafanaURL := grafana["url"] + "/api/datasources" + grafanaUsername := grafana["username"] + grafanaPassword := grafana["password"] + + client := &http.Client{} + postBody, err := json.Marshal(datasource) + if err != nil { + reqLogger.Error(err, "JSON Marshalling error") + return err + } + + req, err := http.NewRequest("POST", grafanaURL, bytes.NewBuffer(postBody)) + if err != nil { + reqLogger.Error(err, "POST REQUEST error") + return err + } + req.Header.Set("Content-Type", "application/json") + req.SetBasicAuth(grafanaUsername, grafanaPassword) + postResp, err := client.Do(req) + if err != nil { + reqLogger.Error(err, "POST RESPONSE error") + return err + } + defer req.Body.Close() + + if postResp.StatusCode == http.StatusOK { + reqLogger.Info("Datasource created") + return nil + } + return err +} |