github.com/stakater/IngressMonitorController@v1.0.103/pkg/monitors/uptimerobot/uptime-status-page.go (about)

     1  package uptimerobot
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	Http "net/http"
     7  	"net/url"
     8  	"strconv"
     9  	"strings"
    10  
    11  	log "github.com/sirupsen/logrus"
    12  
    13  	"github.com/stakater/IngressMonitorController/pkg/config"
    14  	"github.com/stakater/IngressMonitorController/pkg/http"
    15  	"github.com/stakater/IngressMonitorController/pkg/models"
    16  	"github.com/stakater/IngressMonitorController/pkg/util"
    17  )
    18  
    19  type UpTimeStatusPageService struct {
    20  	apiKey string
    21  	url    string
    22  }
    23  
    24  type UpTimeStatusPage struct {
    25  	ID       string
    26  	Name     string
    27  	Monitors []string
    28  }
    29  
    30  func (statusPage *UpTimeStatusPageService) Setup(p config.Provider) {
    31  	statusPage.apiKey = p.ApiKey
    32  	statusPage.url = p.ApiURL
    33  }
    34  
    35  func (statusPageService *UpTimeStatusPageService) Add(statusPage UpTimeStatusPage) (string, error) {
    36  	action := "newPSP"
    37  
    38  	client := http.CreateHttpClient(statusPageService.url + action)
    39  
    40  	body := "api_key=" + statusPageService.apiKey + "&format=json&friendly_name=" + url.QueryEscape(statusPage.Name)
    41  
    42  	if statusPage.Monitors != nil {
    43  		monitors := strings.Join(statusPage.Monitors, "-")
    44  		body += "&monitors=" + monitors
    45  	} else {
    46  		body += "&monitors=0"
    47  	}
    48  
    49  	response := client.PostUrlEncodedFormBody(body)
    50  
    51  	if response.StatusCode == Http.StatusOK {
    52  		var f UptimeStatusPageResponse
    53  		json.Unmarshal(response.Bytes, &f)
    54  
    55  		if f.Stat == "ok" {
    56  			log.Println("Status Page Added: " + statusPage.Name)
    57  			return strconv.Itoa(f.UptimePublicStatusPage.ID), nil
    58  		} else {
    59  			errorString := "Status Page couldn't be added: " + statusPage.Name
    60  			log.Println(errorString)
    61  			return "", errors.New(errorString)
    62  		}
    63  	} else {
    64  		errorString := "Add Status Page Request failed. Status Code: " + strconv.Itoa(response.StatusCode)
    65  		log.Println(errorString)
    66  		return "", errors.New(errorString)
    67  	}
    68  }
    69  
    70  func (statusPageService *UpTimeStatusPageService) Remove(statusPage UpTimeStatusPage) {
    71  	action := "deletePSP"
    72  
    73  	client := http.CreateHttpClient(statusPageService.url + action)
    74  
    75  	body := "api_key=" + statusPageService.apiKey + "&format=json&id=" + statusPage.ID
    76  
    77  	response := client.PostUrlEncodedFormBody(body)
    78  
    79  	if response.StatusCode == Http.StatusOK {
    80  		var f UptimeStatusPageResponse
    81  		json.Unmarshal(response.Bytes, &f)
    82  
    83  		if f.Stat == "ok" {
    84  			log.Println("Status Page Removed: " + statusPage.Name)
    85  		} else {
    86  			log.Println("Status Page couldn't be removed: " + statusPage.Name)
    87  			log.Println(string(body))
    88  		}
    89  	} else {
    90  		log.Println("Remove Status Page Request failed. Status Code: " + strconv.Itoa(response.StatusCode))
    91  	}
    92  }
    93  
    94  func (statusPageService *UpTimeStatusPageService) AddMonitorToStatusPage(statusPage UpTimeStatusPage, monitor models.Monitor) (string, error) {
    95  	existingStatusPage, err := statusPageService.Get(statusPage.ID)
    96  	if err != nil {
    97  		errorString := "Updated Page Request failed. Error: " + err.Error()
    98  		log.Println(errorString)
    99  		return "", errors.New(errorString)
   100  	}
   101  	if util.ContainsString(existingStatusPage.Monitors, monitor.ID) {
   102  		log.Println("Status Page Already Up To Date: " + statusPage.ID)
   103  		return statusPage.ID, nil
   104  	} else {
   105  		existingStatusPage.Monitors = append(existingStatusPage.Monitors, monitor.ID)
   106  
   107  		action := "editPSP"
   108  
   109  		client := http.CreateHttpClient(statusPageService.url + action)
   110  
   111  		body := "api_key=" + statusPageService.apiKey + "&format=json&id=" + statusPage.ID
   112  
   113  		if existingStatusPage.Monitors != nil {
   114  			monitors := strings.Join(existingStatusPage.Monitors, "-")
   115  			body += "&monitors=" + monitors
   116  		} else {
   117  			body += "&monitors=0"
   118  		}
   119  
   120  		response := client.PostUrlEncodedFormBody(body)
   121  
   122  		if response.StatusCode == Http.StatusOK {
   123  			var f UptimeStatusPageResponse
   124  			json.Unmarshal(response.Bytes, &f)
   125  
   126  			if f.Stat == "ok" {
   127  				log.Println("Status Page Updated: " + statusPage.Name)
   128  				return strconv.Itoa(f.UptimePublicStatusPage.ID), nil
   129  			} else {
   130  				errorString := "Status Page couldn't be updated: " + statusPage.Name
   131  				log.Println(errorString)
   132  				return "", errors.New(errorString)
   133  			}
   134  		} else {
   135  			errorString := "Updated Page Request failed. Status Code: " + strconv.Itoa(response.StatusCode)
   136  			log.Println(errorString)
   137  			return "", errors.New(errorString)
   138  		}
   139  	}
   140  }
   141  
   142  func (statusPageService *UpTimeStatusPageService) RemoveMonitorFromStatusPage(statusPage UpTimeStatusPage, monitor models.Monitor) (string, error) {
   143  	existingStatusPage, err := statusPageService.Get(statusPage.ID)
   144  	if err != nil {
   145  		errorString := "Updated Page Request failed. Error: " + err.Error()
   146  		log.Println(errorString)
   147  		return "", errors.New(errorString)
   148  	}
   149  	existingStatusPage.Monitors = remove(existingStatusPage.Monitors, monitor.ID)
   150  
   151  	action := "editPSP"
   152  
   153  	client := http.CreateHttpClient(statusPageService.url + action)
   154  
   155  	body := "api_key=" + statusPageService.apiKey + "&format=json&id=" + statusPage.ID
   156  
   157  	if existingStatusPage.Monitors != nil && len(existingStatusPage.Monitors) > 0 {
   158  		monitors := strings.Join(existingStatusPage.Monitors, "-")
   159  		body += "&monitors=" + monitors
   160  	} else {
   161  		body += "&monitors=0"
   162  	}
   163  
   164  	response := client.PostUrlEncodedFormBody(body)
   165  
   166  	if response.StatusCode == Http.StatusOK {
   167  		var f UptimeStatusPageResponse
   168  		json.Unmarshal(response.Bytes, &f)
   169  
   170  		if f.Stat == "ok" {
   171  			log.Println("Status Page Updated: " + statusPage.Name)
   172  			return strconv.Itoa(f.UptimePublicStatusPage.ID), nil
   173  		} else {
   174  			errorString := "Status Page couldn't be updated: " + statusPage.Name
   175  			log.Println(errorString)
   176  			return "", errors.New(errorString)
   177  		}
   178  	} else {
   179  		errorString := "Updated Page Request failed. Status Code: " + strconv.Itoa(response.StatusCode)
   180  		log.Println(errorString)
   181  		return "", errors.New(errorString)
   182  	}
   183  }
   184  
   185  func (statusPageService *UpTimeStatusPageService) Get(ID string) (*UpTimeStatusPage, error) {
   186  	action := "getPsps"
   187  
   188  	client := http.CreateHttpClient(statusPageService.url + action)
   189  
   190  	body := "api_key=" + statusPageService.apiKey + "&format=json&logs=1" + "&psps=" + ID
   191  
   192  	response := client.PostUrlEncodedFormBody(body)
   193  
   194  	if response.StatusCode == Http.StatusOK {
   195  		var f UptimeStatusPagesResponse
   196  		json.Unmarshal(response.Bytes, &f)
   197  
   198  		if f.StatusPages != nil {
   199  			for _, statusPage := range f.StatusPages {
   200  				return UptimeStatusPageToBaseStatusPageMapper(statusPage), nil
   201  			}
   202  		}
   203  
   204  		return nil, nil
   205  	}
   206  
   207  	errorString := "GetByName Request failed for ID: " + ID + ". Status Code: " + strconv.Itoa(response.StatusCode)
   208  
   209  	log.Println(errorString)
   210  	return nil, errors.New(errorString)
   211  }
   212  
   213  func (statusPageService *UpTimeStatusPageService) GetAllStatusPages(name string) ([]UpTimeStatusPage, error) {
   214  	statusPages := []UpTimeStatusPage{}
   215  	action := "getPsps"
   216  
   217  	client := http.CreateHttpClient(statusPageService.url + action)
   218  
   219  	body := "api_key=" + statusPageService.apiKey + "&format=json&logs=1"
   220  
   221  	response := client.PostUrlEncodedFormBody(body)
   222  
   223  	if response.StatusCode == Http.StatusOK {
   224  		var f UptimeStatusPagesResponse
   225  		err := json.Unmarshal(response.Bytes, &f)
   226  
   227  		if err == nil && len(f.StatusPages) > 0 {
   228  			for _, statusPage := range f.StatusPages {
   229  				if statusPage.FriendlyName == name {
   230  					sp := UptimeStatusPageToBaseStatusPageMapper(statusPage)
   231  					statusPages = append(statusPages, *sp)
   232  				}
   233  			}
   234  			return statusPages, nil
   235  		}
   236  
   237  		return nil, nil
   238  	}
   239  
   240  	errorString := "GetAllStatusPages Request failed for: " + name + ". Status Code: " + strconv.Itoa(response.StatusCode)
   241  
   242  	log.Println(errorString)
   243  	return nil, errors.New(errorString)
   244  }
   245  
   246  func (statusPageService *UpTimeStatusPageService) GetStatusPagesForMonitor(ID string) ([]string, error) {
   247  	IDint, _ := strconv.Atoi(ID)
   248  
   249  	var matchingStatusPageIds []string
   250  	var f UptimeStatusPagesResponse
   251  
   252  	// Initial dummy values
   253  	f.Pagination.Limit = -1
   254  	f.Pagination.Total = 0
   255  	f.Pagination.Offset = 0
   256  	f.StatusPages = []UptimePublicStatusPage{}
   257  
   258  	action := "getPsps"
   259  
   260  	client := http.CreateHttpClient(statusPageService.url + action)
   261  
   262  	if f.StatusPages != nil {
   263  		for f.Pagination.Limit < f.Pagination.Total {
   264  
   265  			body := "api_key=" + statusPageService.apiKey + "&format=json&logs=1&offset=" + strconv.Itoa(f.Pagination.Offset)
   266  
   267  			response := client.PostUrlEncodedFormBody(body)
   268  
   269  			if response.StatusCode == Http.StatusOK {
   270  
   271  				json.Unmarshal(response.Bytes, &f)
   272  
   273  				for _, statusPage := range f.StatusPages {
   274  					if util.ContainsInt(statusPage.Monitors, IDint) {
   275  						matchingStatusPageIds = append(matchingStatusPageIds, strconv.Itoa(statusPage.ID))
   276  					}
   277  				}
   278  			}
   279  		}
   280  		return matchingStatusPageIds, nil
   281  	}
   282  
   283  	errorString := "GetStatusPagesForMonitor Request failed for ID: " + ID
   284  
   285  	log.Println(errorString)
   286  	return nil, errors.New(errorString)
   287  }
   288  
   289  func remove(s []string, i string) []string {
   290  	j := 0
   291  	for _, n := range s {
   292  		if n != i {
   293  			s[j] = n
   294  			j++
   295  		}
   296  	}
   297  	s = s[:j]
   298  	return s
   299  }