ops_simple_monitor.go raw

   1  // Copyright 2022-2025 The sacloud/iaas-api-go Authors
   2  //
   3  // Licensed under the Apache License, Version 2.0 (the "License");
   4  // you may not use this file except in compliance with the License.
   5  // You may obtain a copy of the License at
   6  //
   7  //      http://www.apache.org/licenses/LICENSE-2.0
   8  //
   9  // Unless required by applicable law or agreed to in writing, software
  10  // distributed under the License is distributed on an "AS IS" BASIS,
  11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12  // See the License for the specific language governing permissions and
  13  // limitations under the License.
  14  
  15  package fake
  16  
  17  import (
  18  	"context"
  19  	"time"
  20  
  21  	"github.com/sacloud/iaas-api-go"
  22  	"github.com/sacloud/iaas-api-go/types"
  23  )
  24  
  25  // Find is fake implementation
  26  func (o *SimpleMonitorOp) Find(ctx context.Context, conditions *iaas.FindCondition) (*iaas.SimpleMonitorFindResult, error) {
  27  	results, _ := find(o.key, iaas.APIDefaultZone, conditions)
  28  	var values []*iaas.SimpleMonitor
  29  	for _, res := range results {
  30  		dest := &iaas.SimpleMonitor{}
  31  		copySameNameField(res, dest)
  32  		values = append(values, dest)
  33  	}
  34  	return &iaas.SimpleMonitorFindResult{
  35  		Total:          len(results),
  36  		Count:          len(results),
  37  		From:           0,
  38  		SimpleMonitors: values,
  39  	}, nil
  40  }
  41  
  42  // Create is fake implementation
  43  func (o *SimpleMonitorOp) Create(ctx context.Context, param *iaas.SimpleMonitorCreateRequest) (*iaas.SimpleMonitor, error) {
  44  	result := &iaas.SimpleMonitor{}
  45  	copySameNameField(param, result)
  46  	fill(result, fillID, fillCreatedAt)
  47  
  48  	result.Name = param.Target
  49  	result.Class = "simplemon"
  50  	result.Availability = types.Availabilities.Available
  51  	result.SettingsHash = "settingshash"
  52  	if result.DelayLoop == 0 {
  53  		result.DelayLoop = 60
  54  	}
  55  	if result.MaxCheckAttempts == 0 {
  56  		result.MaxCheckAttempts = 3
  57  	}
  58  	if result.RetryInterval == 0 {
  59  		result.RetryInterval = 10
  60  	}
  61  	if result.NotifyInterval == 0 {
  62  		result.NotifyInterval = 7200
  63  	}
  64  
  65  	putSimpleMonitor(iaas.APIDefaultZone, result)
  66  	return result, nil
  67  }
  68  
  69  // Read is fake implementation
  70  func (o *SimpleMonitorOp) Read(ctx context.Context, id types.ID) (*iaas.SimpleMonitor, error) {
  71  	value := getSimpleMonitorByID(iaas.APIDefaultZone, id)
  72  	if value == nil {
  73  		return nil, newErrorNotFound(o.key, id)
  74  	}
  75  	dest := &iaas.SimpleMonitor{}
  76  	copySameNameField(value, dest)
  77  	return dest, nil
  78  }
  79  
  80  // Update is fake implementation
  81  func (o *SimpleMonitorOp) Update(ctx context.Context, id types.ID, param *iaas.SimpleMonitorUpdateRequest) (*iaas.SimpleMonitor, error) {
  82  	value, err := o.Read(ctx, id)
  83  	if err != nil {
  84  		return nil, err
  85  	}
  86  	copySameNameField(param, value)
  87  	fill(value, fillModifiedAt)
  88  	if value.DelayLoop == 0 {
  89  		value.DelayLoop = 60
  90  	}
  91  	if value.MaxCheckAttempts == 0 {
  92  		value.MaxCheckAttempts = 3
  93  	}
  94  	if value.RetryInterval == 0 {
  95  		value.RetryInterval = 10
  96  	}
  97  	if value.NotifyInterval == 0 {
  98  		value.NotifyInterval = 7200
  99  	}
 100  	putSimpleMonitor(iaas.APIDefaultZone, value)
 101  	return value, nil
 102  }
 103  
 104  // UpdateSettings is fake implementation
 105  func (o *SimpleMonitorOp) UpdateSettings(ctx context.Context, id types.ID, param *iaas.SimpleMonitorUpdateSettingsRequest) (*iaas.SimpleMonitor, error) {
 106  	value, err := o.Read(ctx, id)
 107  	if err != nil {
 108  		return nil, err
 109  	}
 110  	copySameNameField(param, value)
 111  	fill(value, fillModifiedAt)
 112  	if value.DelayLoop == 0 {
 113  		value.DelayLoop = 60
 114  	}
 115  	if value.NotifyInterval == 0 {
 116  		value.NotifyInterval = 7200
 117  	}
 118  	putSimpleMonitor(iaas.APIDefaultZone, value)
 119  	return value, nil
 120  }
 121  
 122  // Delete is fake implementation
 123  func (o *SimpleMonitorOp) Delete(ctx context.Context, id types.ID) error {
 124  	_, err := o.Read(ctx, id)
 125  	if err != nil {
 126  		return err
 127  	}
 128  
 129  	ds().Delete(o.key, iaas.APIDefaultZone, id)
 130  	return nil
 131  }
 132  
 133  // MonitorResponseTime is fake implementation
 134  func (o *SimpleMonitorOp) MonitorResponseTime(ctx context.Context, id types.ID, condition *iaas.MonitorCondition) (*iaas.ResponseTimeSecActivity, error) {
 135  	_, err := o.Read(ctx, id)
 136  	if err != nil {
 137  		return nil, err
 138  	}
 139  	now := time.Now().Truncate(time.Second)
 140  	m := now.Minute() % 5
 141  	if m != 0 {
 142  		now.Add(time.Duration(m) * time.Minute)
 143  	}
 144  
 145  	res := &iaas.ResponseTimeSecActivity{}
 146  	for i := 0; i < 5; i++ {
 147  		res.Values = append(res.Values, &iaas.MonitorResponseTimeSecValue{
 148  			Time:            now.Add(time.Duration(i*-5) * time.Minute),
 149  			ResponseTimeSec: float64(random(1000)),
 150  		})
 151  	}
 152  
 153  	return res, nil
 154  }
 155  
 156  // HealthStatus is fake implementation
 157  func (o *SimpleMonitorOp) HealthStatus(ctx context.Context, id types.ID) (*iaas.SimpleMonitorHealthStatus, error) {
 158  	_, err := o.Read(ctx, id)
 159  	if err != nil {
 160  		return nil, err
 161  	}
 162  
 163  	return &iaas.SimpleMonitorHealthStatus{
 164  		LastCheckedAt:       time.Now(),
 165  		LastHealthChangedAt: time.Now(),
 166  		Health:              types.SimpleMonitorHealth.Up,
 167  	}, nil
 168  }
 169